< 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 any 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






  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












    Runtime polymorphism is also known as dynamic method dispatch during runtime. Runtime polymorphism takes place in fulfilment of these conditions -
    //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





    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 >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2