• The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages.


  • This principle can also be applied to object-oriented programming and languages like the Java language.


  • Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.


  • Polymorphism can be demonstrated with a minor modification to the Bicycle class.


  • For example, a printDescription method could be added to the class that displays all the data currently stored in an instance.


  •  
    
    
    class Bicycle
    {
    public void printDescription(){
        System.out.println("Bicycle");
    }
    }
    
    
  • To demonstrate polymorphic features in the Java language, extend the Bicycle class with a MountainBike and a RoadBike class.


  • Here is the updated class:


  •  
    public class MountainBike extends Bicycle {
        
      
      
    
        public void printDescription() {
            super.printDescription();
            System.out.println("MountainBike");
        }
    } 
    
    
  • Note the overridden printDescription method.


  • Next, create the RoadBike class.


  • Because road or racing bikes have skinny tires, add an attribute to track the tire width. Here is the RoadBike class:


  •  
    public class RoadBike extends Bicycle{
       
       
    
        public void printDescription(){
            super.printDescription();
            System.out.println("RoadBike");
        }
        
    }
    
    
  • Note that once again, the printDescription method has been overridden.


  • To summarize, there are three classes: Bicycle, MountainBike, and RoadBike.


  • The two subclasses override the printDescription method and print unique information.


  • Here is a test program that creates three Bicycle variables. Each variable is assigned to one of the three bicycle classes. Each variable is then printed.


  •  
    public class TestBikes {
      public static void main(String[] args){
        Bicycle bike01, bike02, bike03;
    
        bike01 = new Bicycle();
        bike02 = new MountainBike();
        bike03 = new RoadBike();
    
        bike01.printDescription();
        bike02.printDescription();
        bike03.printDescription();
      }
    }
    
    
    
    
    The following is the output from the test program:
    
    Bicycle
    MountainBike
    RoadBike
    
    
  • The Java virtual machine (JVM) calls the appropriate method for the object that is referred to in each variable.


  • It does not call the method that is defined by the variable's type.


  • This behavior is referred to as virtual method invocation and demonstrates an aspect of the important polymorphism features in the Java language.


  • If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super.


  • You can also use super to refer to a hidden field. Consider this class, Superclass:


  •  
    public class Superclass {
    
        public void printMethod() {
            System.out.println("Printed in Superclass.");
        }
    }
    
    
  • Here is a subclass, called Subclass, that overrides printMethod():


  •  
    public class Subclass extends Superclass {
    
        // overrides printMethod in Superclass
        public void printMethod() {
            super.printMethod();
            System.out.println("Printed in Subclass");
        }
        public static void main(String[] args) {
            Subclass s = new Subclass();
            s.printMethod();    
        }
    }
    
    
    
  • Within Subclass, the simple name printMethod() refers to the one declared in Subclass, which overrides the one in Superclass.


  • So, to refer to printMethod() inherited from Superclass, Subclass must use a qualified name, using super as shown.


  •  
    Compiling and executing Subclass prints the following:
    
    Printed in Superclass.
    Printed in Subclass
    
    
    
  • The following example illustrates how to use the super keyword to invoke a superclass's constructor.


  • In example given below, the object created in the class Subclass leads to calling of constructor of Subclass. When Subclass constructor is executed, the instructor super() calls Superclass() constructor.


  •  
    class Superclass {
        
        Superclass()
        {
            System.out.println("Constructor in Superclass");
        }
       
    }
    
    class Subclass extends Superclass {
    
    
    
        Subclass()
        {
            super();
            System.out.println("Constructor in Subclass");
        }
       
        public static void main(String[] args) {
            Subclass s = new Subclass();
              
        }
    }
    
    
    Output:
    Constructor in Superclass                                                                          
    Constructor in Subclass
    
    
    
  • Invocation of a superclass constructor must be the first line in the subclass constructor.


  • The syntax for calling a superclass constructor is


  •  
    super();  
    or:
    super(parameter list);
    
    
    
    
  • With super(), the superclass no-argument constructor is called.


  • With super(parameter list), the superclass constructor with a matching parameter list is called.


  • Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.


  • The Java compiler automatically inserts a call to the no-argument constructor of the superclass. The below code example demonstrates this.


  •  
    class Superclass {
        
        Superclass()
        {
            System.out.println("Constructor in Superclass");
        }
       
    }
    
    class Subclass extends Superclass {
    
    
    
        Subclass()
        {
           
            System.out.println("Constructor in Subclass");
        }
       
        public static void main(String[] args) {
            Subclass s = new Subclass();
              
        }
    }
    
    
    Output:
    Constructor in Superclass                                                                          
    Constructor in Subclass
    
    
    
  • If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.