Java Reference
In-Depth Information
Polymorphism
The word “polymorphism” has its root in two Greek words: “poly” (means many) and “morphos” (means form).
In programming, polymorphism is the ability of an entity (e.g. variable, class, method, object, code, parameter,
etc.) to take on different meanings in different contexts. The entity that takes on different meanings is known as
a polymorphic entity. Various types of polymorphism exist. Each type of polymorphism has a name that usually
indicates how that type of polymorphism is achieved in practice. The proper use of polymorphism results in generic
and reusable code. The purpose of polymorphism is writing reusable and maintainable code by writing code in terms
of a generic type that works for many types (or ideally all types).
Polymorphism can be categorized in the following two categories:
Ad hoc polymorphism
Universal polymorphism
If the types for which a piece of code works are finite and all those types must be known when the code is written,
it is known as ad hoc polymorphism. Ad hoc polymorphism is also known as apparent polymorphism because it
is not a polymorphism in a true sense. Some computer science purists do not consider ad hoc polymorphism as
polymorphism at all. Ad hoc polymorphism is divided into two types: overloading polymorphism and coercion
polymorphism.
If a piece of code is written in such a way that it works for infinite number of types (will also work for new types
not known at the time the code is written), it is called universal polymorphism. In universal polymorphism, the same
code works on many types, whereas in ad hoc polymorphism, different implementations of code are provided for
different types giving an apparent impression of polymorphism. Universal polymorphism is divided into two types:
inclusion polymorphism and parametric polymorphism.
Overloading Polymorphism
Overloading is an ad hoc polymorphism. Overloading results when a method (called a method in Java and a function
in other languages) or an operator has at least two definitions that work on different types. In such cases, the same
method or operator name is used for their different definitions. That is, the same name exhibits many behaviors and
hence the polymorphism. Such methods and operators are called overloaded methods and overloaded operators.
Java lets you define overloaded methods. Java has some overloaded operators. Java does not let you overload an
operator for an ADT. That is, you cannot provide a new definition for an operator in Java.
Listing 1-7 shows code for a class named MathUtil .
Listing 1-7. An Example of an Overloaded Method in Java
package com.jdojo.concepts;
public class MathUtil {
public static int max(int n1, int n2) {
/* Code to determine the maximum of two integers goes here */
}
public static double max(double n1, double n2) {
/* Code to determine the maximum of two floating-point numbers goes here */
}
public static int max(int[] num) {
/* Code to determine the maximum of an array of int goes here */
}
}
 
Search WWH ::




Custom Search