POLYMORPHISM

JAVA NOTES   The term polymorphic is derived from the Greek words poly (many) and morphic (shapes). From a programming perspective, we use the term polymorphism to describe a single value which may take on several different definitions. 

In this instance, we will describe three different types of polymorphism:

  • ad hoc polymorphism
  • inclusion polymorphism
  • assignment polymorphism

 

AD HOC POLYMORPHISM

Commonly known as overloading, this type of polymorphism occurs when a single function name has several different definitions.  For example, let's assume we have the following class definition:

Class Number {

public void printNumber (int i);

public void printNumber (double d, int i);

public void printNumber (char c);

}

Let's assume our function printNumber will accept some type of variable(s) as its argument and print out this variable to the screen.  Basically, the purpose of these three functions is the same however, we want to pass in different data types and perform certain actions based upon what those data types are.  For this reason, we overload the printNumber function to accept different parameters.  One version of the function for example, will accept an integer and may display that integer on the screen.  Another form of this function can accept a character and we may provide some instructions to display the numeric version of that character.  Finally, a third version may accept multiple variables and display multiple variables to the screen.

By overloading a function, we basically tell the compiler "I'm using the same function name to define several different functions."   

 

INCLUSION POLYMORPHISM

Commonly known as overriding, this form of polymorphism is similar to ad hoc however, overriding occurs in a different context.  Specifically, it is seen in a superclass - subclass relationship.  That is, when one class (subclass)  inherits from another class (superclass), the subclass naturally inherits functions from the superclass but as a programmer, you may want the inherited function to perform a different operation than it did in the superclass.  Take the following class definitions for example:

Class Vehicle {

public void measureEmissions (double d);

}

Class Ford extends Vehicle {

public void measureEmissions (double d);

}

In this case, let's assume the measureEmissions function in our superclass Vehicle has some instructions in it to determine the emissions of a generic vehicle and compare it to some standard d.  Now, if we have a specific vehicle in mind, modeled by our Ford class, we may have some different instructions/calculations to perform for the Ford class.  Ford inherits the method measureEmissions however, it overrides that method and creates its own set of instructions to be carried out.

Notice that the function signatures are the same for both classes.  This is necessary when overriding a method (as opposed to overloading where the signatures may be different).

 

 

extends

keyword used in Java to designate a subclass

Class Dog extends Animal { .... }

In this example, the class Dog is inheriting from the class Animal.