- 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
How can we restrict Generics (type parameter) to sub classes of a particular class in Java?
Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.
You can declare a bound parameter just by extending the required class with the type-parameter, within the angular braces as −
class Sample <T extends Number>
Example
In the following Java example the generic class Sample restricts the type parameter to the sub classes of the Number classes using the bounded parameter.
class Sample <T extends Number>{ T data; Sample(T data){ this.data = data; } public void display() { System.out.println("Data value is: "+this.data); } } public class BoundsExample { public static void main(String args[]) { Sample<Integer> obj1 = new Sample<Integer>(20); obj1.display(); Sample<Double> obj2 = new Sample<Double>(20.22d); obj2.display(); Sample<Float> obj3 = new Sample<Float>(125.332f); obj3.display(); } }
Output
Data value is: 20 Data value is: 20.22 Data value is: 125.332
Now, if you pass other types as parameters to this class (say, String for example) a compile time error will be generated.
Example
public class BoundsExample { public static void main(String args[]) { Sample<Integer> obj1 = new Sample<Integer>(20); obj1.display(); Sample<Double> obj2 = new Sample<Double>(20.22d); obj2.display(); Sample<String> obj3 = new Sample<String>("Krishna"); obj3.display(); } }
Compile time error
BoundsExample.java:16: error: type argument String is not within bounds of type-variable T Sample<String> obj3 = new Sample<String>("Krishna"); ^ where T is a type-variable: T extends Number declared in class Sample BoundsExample.java:16: error: type argument String is not within bounds of type-variable T Sample<String> obj3 = new Sample<String>("Krishna"); ^ where T is a type-variable: T extends Number declared in class Sample 2 errors
Multiple bounds
You can also list multiple types as bounded parameters using the extends keyword separating them using “&” thee, The (type) parameter passed to this class should be subtype of all the specified classes.
Example
In the following example we are setting the Number and Comparable types of upper bounds i.e. this class accepts types which are sub classes of both classes.
class Sample <T extends Number & Comparable<T> >{ T data; Sample(T data){ this.data = data; } public void display() { System.out.println("Data value is: "+this.data); } } public class BoundsExample { public static void main(String args[]) { Sample<Integer> obj1 = new Sample<Integer>(22); obj1.display(); Sample<Double> obj2 = new Sample<Double>(20.22d); obj2.display(); } }
Output
Data value is: 22 Data value is: 20.22
If you pass an AtomicInteger class as a parameter to the Sample class, since it is not a sub type of the comparable class, a compile time error will be generated
Example
import java.util.concurrent.atomic.AtomicInteger; class Sample <T extends Number & Comparable<T> >{ T data; Sample(T data){ this.data = data; } public void display() { System.out.println("Data value is: "+this.data); } } public class BoundsExample { public static void main(String args[]) { Sample<Integer> obj1 = new Sample<Integer>(22); obj1.display(); Sample<Double> obj2 = new Sample<Double>(20.22d); obj2.display(); Sample<AtomicInteger> obj3 = new Sample<AtomicInteger>(124); obj3.display(); } }
Compile time error
BoundsExample.java:16: error: type argument String is not within bounds of type-variable T Sample<String> obj3 = new Sample<String>("Krishna"); ^ where T is a type-variable: T extends Number declared in class Sample BoundsExample.java:16: error: type argument String is not within bounds of type-variable T Sample<String> obj3 = new Sample<String>("Krishna"); ^ where T is a type-variable: T extends Number declared in class Sample 2 errors
- Related Articles
- Is it mandatory to use T for type-parameter, while defining Generics classes/methods in Java?
- How to convert a sub class variable into a super class type in Java?
- How to convert a super class variable into a sub class type in Java
- Can you create an array of Generics type in Java?
- How can we create a JPopupMenu with a sub menu in Java?
- How can we sort a JTable on a particular column in Java?
- How can we add multiple sub-panels to the main panel in Java?
- How can we use a diamond operator with anonymous classes in Java 9?
- Why do we need generics in Java?
- Can super class reference variable hold sub class's object in java?
- How can we use the StringTokenizer class in Java?
- Is it possible to instantiate Type-parameter in Java?
- How can we implement the SubmissionPublisher class in Java 9?
- Can we define a class inside a Java interface?
- How we can extend multiple Python classes in inheritance?
