Sometimes, a class or a method needs to place restrictions on type variables. Here is atypical example. We want to compute the smallest element of an array:
But there is a problem. Look inside the code of the min method. The variable smallest hastype T, which means that it could be an object of an arbitrary class. How do we knowthat the class to which T belongs has a compareTo method? The solution is to restrict T to a class that implements the Comparable interface—a standardinterface with a single method, compareTo. You achieve this by giving a bound for the typevariable T:
Actually, the Comparable interface is itself a generic type. For now, we will ignore that complexityand the warnings that the compiler generates. Now, the generic min method can only be called with arrays of classes that implement theComparable interface, such as String, Date, and so on. Calling min with a Rectangle array is acompile-time error because the Rectangle class does not implement Comparable.
C++ NOTE: In C++, you cannot restrict the types of template parameters. If a programmer instantiates a template with an inappropriate type, an (often obscure) error message isreported inside the template code. You may wonder why you use the extends keyword rather than the implements keyword inthis situation—after all, Comparable is an interface. The notation <T extends BoundingType>expresses that T should be a subtype of the bounding type. Both T and the bounding typecan be either a class or an interface. The extends keyword was chosen because it is a reasonable approximation of the subtype concept, and the Java designers did not want toadd a new keyword (such as sub) to the language.
A type variable or wildcard can have multiple bounds. For example:
The bounding types are separated by ampersands (&) because commas are used to separate type variables. As with Java inheritance, you can have as many interfacesupertypes as you like, but at most one of the bounds can be a class. If you have a class as a bound, it must be the first one in the bounds list.
In the next sample program (Listing below), we rewrite the minmax method to be generic. The method computes the minimum and maximum of a generic array, returning a Pair<T>.
Core Java Related Interview Questions
|J2EE Interview Questions||Core Java Interview Questions|
|JDBC Interview Questions||JSP Interview Questions|
|Android Interview Questions||JavaServer Faces (JSF) Interview Questions|
|Java collections framework Interview Questions||Java 8 Interview Questions|
|Java Collections Interview Questions||Java Exception Handling Interview Questions|
|Java Concurrency Interview Questions||Java Serialization Interview Questions|
|Java Programmer Interview Questions||Java Inheritance Interview Questions|
|Java IO Interview Questions||Object Oriented Programming in PHP Interview Questions|
Core Java Tutorial
An Introduction To Java
The Java Programming Environment
Fundamental Programming Structures In Java
Objects And Classes
Interfaces And Inner Classes
User Interface Components With Swing
Deploying Applications And Applets
Exceptions, Logging, Assertions, And Debugging
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.