runtime polymorphism in java

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello all, in previous article we learned about compile time polymorphism and how it is used in method overloading. In this article we will see about runtime polymorphism. Runtime polymorphism is achieved using method overriding. Rules for method overriding are as follows.

  • Overriding method argument list must match the overridden method
  • The return type must be the same or subtype of overridden method
  • access level cannot be more restrictive than overridden method

To understand how runtime polymorphism works with method overriding, consider following cases:

  • Greeting class has a method called  greet()  which is used to give general greetings
  • BirthdayGreeting extends Greeting
  • BirthdayGreeting class has a method called greet()  which is overriding greet()  method in Greeting class and wishes happy birthday.

BirthdayGreeting class.

Now create Greeter class to test overriding

This produces output

This is what happens here.

  • The statement  Greeting greeting = new Greeting();  creates a reference variable of type  Greeting  and also and object of type Greeting .
  • The statement  Greeting birthdayGreeting = new BirthdayGreeting();  creates a reference variable of type Greeting but object of type BirthdayGreeting .
  • Method call  greeting.greet()  prints "General Greetings"  as expected
  • Method call birthdayGreeting.greet()  prints "Happy Birthday" .
    • because only the reference variable is of type Greeting .
    • Statement  Greeting birthdayGreeting = new BirthdayGreeting();  means that reference variable is of type Greeting but the actual object at runtime will be of type BirthdayGreeting(); . This is called as polymorphic reference.
    • A reference variable of type Greeting can reference to BirthdayGreeting object because  BirthdayGreeting extends Greeting  and polymorphic reference can be used for subclass references.
    • So the method call for  greet()  is resolved at runtime based on type of object.


Remember that the overriding method to be called at runtime is decided at runtime based on the type of object and this is called as runtime polymorphism.





Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

12 thoughts on “runtime polymorphism in java

  • November 22, 2013 at 9:10 pm

    Greeting birthdayGreeting = new Greeting();

    can we take the reference of sub class for the object of super class .

    • April 18, 2016 at 12:41 am

      that is call downcasting .yes we can do that with the help of instanceof Operator.

  • November 22, 2013 at 9:15 pm

    sorry, above statement for object creating is true but Question is different

    my Question is for this statement-
    BirthdayGreeting birthdayGreeting = new Greeting();
    can we take the reference of sub class for the object of super class .

    • November 23, 2013 at 12:54 am

      You cannot do that. The compiler will complain when you do that. You can try out a program for that.

      • April 18, 2014 at 5:34 pm

        Can you please explain why we can’t keep child class reference in parent class object?

        • April 19, 2014 at 4:51 pm

          That is because a child class can have more methods those can’t be in Parent class. Consider an example where Vehicle class is Super class of Bike class. All vehicles do not have gears and Bike class has shiftGear() method which is not present in Vehicle class. Suppose you create the Vehicle instance as Bike bike = new Vehicle(); then what compiler thinks is that the object created i.e. Vehicle instance can access all methods of Bike class as our reference variable is of type Bike. If this type of instantiation was allowed, it would have meant Vehicle object is accessing method of Bike object which is not possible. Hence java does not allow this kind of polymorphic reference. You cannot refer a superclass instance using Child type reference variable.

          Does this answer your question?

          • April 20, 2014 at 10:06 am

            Yes. Thank you

  • May 9, 2015 at 10:31 pm

    i understood what is polymorphism.. could you tell me why we need polymorphism?
    why do we use compile time and run time polymorphism?

    • May 11, 2015 at 10:12 am

      We need polymorphism to access multiple types using the same supertype. For example, a reference variable of type List can refer to LinkedList or ArrayList. compile time and run time polymorphism are the concepts provided by java language. Its not that we have to use them explicitly. Although you can say that method overloading is compile time polymorhism and method overriding is run time polymorphism.

  • September 5, 2017 at 10:07 pm

    Why we can’t do as below like creating different objects and calling the method as below.
    Greeting greeting=new Greeting();
    BirthdayGreeting bg=new BirthdayGreeting();

    • September 6, 2017 at 12:27 pm

      We can certainly do that. This article simply tries to explain runtime polymorphism.


Leave a Reply

Your email address will not be published. Required fields are marked *