Java Reference
In-Depth Information
Java supports inclusion polymorphism using inheritance, which is a subclassing mechanism. You can define
a method in Java using a formal parameter of a type, for example, Person , and that method can be called on all its
subtypes, for example, Employee , Student , Customer , etc. Suppose you have a method processDetails() as follows:
void processDetails(Person p) {
/* Write code using the formal parameter p, which is of type Person. The same code will
work if an object of any of the subclass of Person is passed to this method.
*/
}
The processDetails() method declares a formal parameter of Person type. You can define any number of
classes that are subclasses of the Person class. The processDetails() method will work for all subclasses of the
Person class. Assume that Employee and Customer are subclasses of the Person class. You can write code like
Person p1 = create a Person object;
Employee e1 = create an Employee object;
Customer c1 = create a Customer object;
processDetails(p1); // Use Person type
processDetails(e1); // Use Employee type, which is a subclass of Person
processDetails(c1); // Use Customer type, which is a subclass of Person
The effect of the subtyping rule is that the supertype includes (hence the name inclusion) all values that belong
to its subtypes. A piece of code is called universally polymorphic only if it works on an infinite number of types.
In the case of inclusion polymorphism, the number of types for which the code works is constrained but infinite.
The constraint is that all types must be the subtype of the type in whose term the code is written. If there is no
restriction on how many subtypes a type can have, the number of subtypes is infinite (at least in theory). Note that
inclusion polymorphism not only lets you write reusable code, it also lets you write extensible and flexible code. The
processDetails() method works on all subclasses of the Person class. It will keep working for all subclasses of the
Person class, which will be defined in future, without any modifications. Java uses other mechanisms, like method
overriding and dynamic dispatch (also called late binding), along with subclassing rules to make the inclusion
polymorphism more effective and useful.
Parametric Polymorphism
Parametric is a universal polymorphism. It is also called “true” polymorphism because it lets you write true generic
code that works for any types (related or unrelated). Sometimes it is also referred to as generics. In parametric
polymorphism, a piece of code is written in such a way that it works on any type. Contrast parametric polymorphism
with inclusion polymorphism. In inclusion polymorphism, code is written for one type and it works for all of its
subtypes. It means all types for which the code works in inclusion polymorphism are related by a supertype-subtype
relationship. However, in parametric polymorphism, the same code works for all types, which are not necessarily
related. Parametric polymorphism is achieved by using a type variable when writing the code, rather than using any
specific type. The type variable assumes a type for which the code needs to be executed. Java supports parametric
polymorphism since Java 5 through generics. Java supports polymorphic entity (e.g. parameterized classes) as well as
polymorphic method (parameterized methods) that use parametric polymorphism.
All collection classes as of Java 5 have been retrofitted to use generics (parametric polymorphism is achieved
in Java using generics). You can write code using generics as shown below. This code uses a List object as a list of
String type and a List object as a list of Integer type. Using generics, you can treat a List object as a list of any type
in Java. Note the use of <XXX> (angle brackets) in code to specify the type for which you want to instantiate the
List object.
 
Search WWH ::




Custom Search