Categories: Core JAVATutorial

Runtime Polymorphism in Java

Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.

Upcasting:

When reference variable of Parent class refers to the object of Child class, it is known as upcasting.
For example:

class A{}
class B extends A{}
class Test{
   public static void main(String args[]){
   A a=new B();//upcasting
  }
}

Example of Runtime Polymorphism:

In this example, we are creating two classes Bicycle and HondaShine. HondaShine class extends Bicycle class and overrides its run() method. We are calling the run method by the reference variable of Parent class. Since it refers to the subclass object and subclass method overrides the Parent class method, subclass method is invoked at runtime. Since it is determined by the compiler, which method will be invoked at runtime, so it is known as runtime polymorphism.

 

class Bicycle{
   void run(){
   System.out.println("bicycle is running");
   }
 }
 class HondaShine extends Bicycle{
   void run(){
     System.out.println("shine is running safely with 70km");
    }
 
   public static void main(String args[]){
     Bicycle b = new HondaShine();//upcasting
     b.run();
   }
 }

 

Output:
shine is running safely with 70km

Runtime Polymorphism with data member:

Method is overriden not the datamembers, so runtime polymorphism can’t be achieved by data members.
In the example given below, both the classes have a datamember speedlimit, we are accessing the datamember by the reference variable of Parent class which refers to the subclass object. Since we are accessing the datamember which is not overridden, hence it will access the datamember of Parent class always.

class Bicycle{
  int speedlimit=100;
 }
 class HondaShine extends Bicycle{
  int speedlimit=160;
 
  public static void main(String args[]){
   Bicycle obj=new HondaShine();
   System.out.println(obj.speedlimit);
 }
}

 

Output:
100

Note: Runtime polymorphism can’t be achieved by data members.

<<Previous <<   || Index ||   >>Next >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago