Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.7. Polymorphism

4.7. Polymorphism
Last updated: 23 January 2013.

To illustrate the concept of polymorphism, let's take a look at the class Aircraft shown below. It is a modified version of the class Aircraft used in the previous tutorials. It only has a single method named printType that prints its type to the standard output:

public class Aircraft {

   
public void printType(){
         
System.out.println("Object type : Aircraft");
   
}

}

The class Plane shown below is a subclass of the class Aircraft and overrides the method printType:

public final class Plane extends Aircraft {

   
public void printType(){
         
System.out.println("Object type : Plane");
   
}

}

Likewise, the class Helicopter shown below extends the class Aircraft and overrides the method printType:

public final class Helicopter extends Aircraft {

   
public void printType(){
         
System.out.println("Object type : Helicopter");
   
}

}

Now let's take a look at the following statements:

Aircraft aircraft1 = new Aircraft();
Aircraft aircraft2 =
new Plane();
Aircraft aircraft3 =
new Helicopter();

aircraft1.printType
();
aircraft2.printType
();
aircraft3.printType
();

If you run the code above, you will get this output:

Object type: Aircraft
Object type: Plane
Object type: Helicopter

As you can see, the variable aircraft2 is declared as an Aircraft but it is instantiated as a Plane at runtime. That statement compiles because Plane is a subclass of Aircraft. When I called the method printType on the object aircraft2, it was not the method printType provided by the class Aircraft that was executed but rather the method printType provided by the class Plane.

This is one aspect of polymorphism in Java that allows you to declare a super class variable and instantiate it as a subclass variable. The type of the 3 variables declared above is Aircraft but the method printType does something different depending on the object it is called upon. Each of the 3 variables has a specific behaviour but you can pass any of them to a method that takes a parameter of type Aircraft.

Another aspect of polymorphism is that you can use an interface as a variable type. For example, let's return to the interface AircraftInterface that I have used in the previous tutorials:

public interface AircraftInterface {

   
int MINIMUM_TAKEOFF_DISTANCE = 0;
   
int MAXIMUM_CREW_COUNT = 30;

   
public boolean land();
   
public boolean takeOff();

}

The class Plane now implements the interface AircraftInterface:

public final class Plane implements AircraftInterface {

   
public boolean land(){
         
// The implementation goes here
   
}

   
public boolean takeOff(){
         
// The implementation goes here
   
}

   
// Other methods
    //...

}

The statement below declares a variable aircraft of type AircraftInterface that will be instantiated as a Plane object at runtime:

AircraftInterface aircraft = new Plane();

The statement is correct because the class Plane implements the interface AircraftInterface. More generally, any class that implements the interface AircraftInterface can be assigned to a variable of type AircraftInterface. You can call the methods land and takeOff on the variable aircraft since its type is AircraftInterface. You can also pass the variable aircraft to a method that takes a parameter of type AircraftInterface.


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.7. Polymorphism
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.8. Packages

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7