- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we throw an object of generic class in java?
Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any data type.
Example
class Student<T>{ T age; Student(T age){ this.age = age; } public void display() { System.out.println("Value of age: "+this.age); } } public class GenericsExample { public static void main(String args[]) { Student<Float> std1 = new Student<Float>(25.5f); std1.display(); Student<String> std2 = new Student<String>("25"); std2.display(); Student<Integer> std3 = new Student<Integer>(25); std3.display(); } }
Output
Value of age: 25.5 Value of age: 25 Value of age: 25
Throwing a generic class object
To create a custom class that is throwable using the throws clause you need to extend the throwable class.
class MyException extends Throwable{ MyException(String msg){ super(msg); } }
Therefore if you need to throw an object of generic type, you should be able to extend the Throwable class from it. But, if you try to do so a compile time error will be generated. Therefore, you cannot throw an object of the Generic class using the throws clause.
Example
class Student<T>extends Throwable{ T age; Student(T age){ this.age = age; } public void display() { System.out.println("Value of age: "+this.age); } } public class GenericsExample { public static void main(String args[]) { } }
Compile time error
GenericsExample.java:1: error: a generic class may not extend java.lang.Throwable class Student<T>extends Throwable{ ^ 1 error
- Related Articles
- Can we create an object of an abstract class in Java?
- Can we create an object for the abstract class in java?
- Can we have generic constructors in Java?
- Can we throw an Unchecked Exception from a static block in java?
- Defining generic method inside Non-generic class in Java
- Can we have multiple type parameters in generic methods in Java?
- Can we write any code after throw statement in Java?
- Can we create an object for an interface in java?
- Can a constructor throw an exception in Java?
- Can the abstract methods of an interface throw an exception in java?
- Can we define an enum inside a class in Java?
- Can we define an interface inside a Java class?
- Get super class of an object in Java
- Can we define a parameterized constructor in an abstract class in Java?
- Can constructor throw exceptions in Java?

Advertisements