Advertisement



< Prev
Next >



Polymorphism in Java





Polymorphism means having many forms. In Java, polymorphism allows us to access an object in multiple ways, such as -
An object accessed in such multiple ways is said to be accessed polymorphically or polymorphic in nature.




Note:


All Java classes extend from Object class, hence it is a parent class or a superclass to all the java classes. That's why a reference variable of Object class can be used to refer an object of any class.




Accessing an object polymorphically


  1. A reference variable can refer to any object of its own class and also an object of its subclass(in inheritance).


  2. class A
    {
    }
    
    class B extends A	//class B is subclass/child class of class A
    {
    }
    
    class Polymorphism
    {
    public static void main(String... ar)
    {
    B ob1 = new B();	//Object of B is accessed by reference variable of its own B class.
    A ob2 = new B();	//Object of B is accessed by reference variable of its superclass, A.
    Object ob3 = new B();	//Object of B is accessed by reference variable of its superclass, Object.
    }
    }


    Program Analysis


      As you may see in the code above, an object of B class is accessed polymorphically by -
    • Reference variable of its own class B and,
    • Reference variable of its superclass A and,
    • Reference variable of the superclass of all classes, Object class.




  3. A reference variable declared as an interface type can refer to any object of the class that has implemented this interface.


  4. interface A
    {
    }
    
    class B implements A	//class B is implementing interface A, hence A is superclass of B
    {
    }
    
    class Polymorphism
    {
    public static void main(String... ar)
    {
    B ob1 = new B();	//Object of B is accessed by reference variable of its own B class.
    A ob2 = new B();	//Object of B is accessed by reference variable of its superclass, A.
    Object ob3 = new B();	//Object of B is accessed by reference variable of its superclass, Object.
    }
    }
    


    Program Analysis


      As you may see in the code just above, the object of B class is accessed polymorphically by -
    • Reference variable of its own class B and,
    • Reference variable of its superclass i.e. interface A and,
    • Reference variable of the superclass of all classes, Object class.



    Advertisement




      Two types of polymorphism


    • Compile-time polymorphism.
    • Runtime polymorphism.




    • Compile-time polymorphism during method overloading.

    • Compile-time polymorphism takes place in the fulfillment of all of these conditions -
      • When a class extends another class by inheritance.
      • Method of a class is overloaded in its subclass.
      • An object of a subclass is accessed polymorphically(using the reference of its superclass), to call on the overloaded method.


      // Compile-time polymorphism - method overloading
      
      class A				//Superclass	
      {
      public void message(A ob)
      {
      System.out.println("A");
      }
      }
      
      class B extends A		//Subclass
      {
      public void message(B ob)       //overloaded message() method
      {
      System.out.println("B");
      }
      }
      
      class Poly
      {
      public static void main(String... ar)
      {
      A ob1 = new B(); 	//ob1 is declared of type A but it is pointing to object of its subclass, B
      ob1.message(ob1);
      }
      }
      


      Output -


      A


      Program Analysis


      • Class A is extended by a class B and its method message() is overloaded in class B.
      • Reference variable ob1 is declared of type A but it is pointing to object of its subclass, B.
      • Using the reference variable ob1, the overloaded method message() is called, passing the reference ob1 of type A.
      • Which message() method is executed, is based on the type of reference variable, passed as an argument to call the message() method, at the compile time.
      • Hence, the ob1 reference of type A is passed as an argument, so the method message() of class A is called.



      Note:


      In compile-time polymorphism, which overloaded version of a method will be executed is decided at compile time, based on the type of reference variable passed to the overloaded method.





    • Runtime polymorphism during method overriding.

    Runtime polymorphism is also known as dynamic method dispatch during runtime. Runtime polymorphism takes place in fulfillment of these conditions -
    • When a class extends another class and,
    • Method of the superclass is overridden in its subclass.
    • An object of subclass is accessed polymorphically to call on the overridden method.


    //Runtime polymorphism or dynamic method dispatch, during method overriding 
    
    
    class Vegetable				//superclass
    {
    public void message()
    {
    System.out.println("Eat vegetables");
    }
    }
    
    
    class Tomato extends Vegetable		//subclass
    {
    public void message()			//method message() of superclass Vegetable is overridden 
    {
    System.out.println("Eat Tomatoes");
    }
    }
    
    class Polymorphism
    {
    public static void main(String... ar)
    {
    Vegetable ob1= new Tomato();		//comment1
    ob1.message();				//comment2
    }
    }
    

    Output -


    Eat Tomatoes


    Program Analysis


    • A class Vegetable is extended by a class Tomato and its method message() is overridden in class Tomato.
    • At comment1, reference variable ob1 declared of type Vegetable, is pointing to an object of Tomato class.
    • At comment2, the reference variables ob2 is calling the overridden method message(). But which message() method is executed, is based on type of the object used to call the message() at the runtime and not at the reference type used to call message() at compile time.
    • Hence, as ob1 of reference type Vegetable is pointing to object type Tomato, that's why at comment2, the method message() of class Tomato is called.



    Note:


    In Runtime polymorphism, which overridden version of a method will be executed is decided at runtime, based on the type of object and not at the reference type at compile time.



    Please share this article -




    < Prev
    Next >
    < Aggregation and Composition
    Overloading >



    Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement