Java OOP Concepts 2 - Run time Polymorphism - Method Overriding

Run Time Polymorphism (Method Overriding) in JAVA


This can be explained as one method or action in many forms.
There are two types of polymorphism in Java.

01. Run time polymorphism - Method overriding.
02. Compile time polymorphism - Method overloading.

In this article I will explain about Run Time Polymorphism

This is also called as  "Dynamic Method Dispatch". There can be multiple methods with same name but the determination of which one to be called is decided at run time.

This is the process of an overridden method is called through the reference variable of a super class.

An example if below which is extracted from http://www.javatpoint.com/runtime-polymorphism-in-java

class Bank{  
int getRateOfInterest(){return 0;}  
}  
  
class SBI extends Bank{  
int getRateOfInterest(){return 8;}  
}  
  
class ICICI extends Bank{  
int getRateOfInterest(){return 7;}  
}  
class AXIS extends Bank{  
int getRateOfInterest(){return 9;}  
}  
  
class Test3{  
public static void main(String args[]){  
Bank b1=new SBI();  // This process is called upcasting.
Bank b2=new ICICI();  
Bank b3=new AXIS();  
System.out.println("SBI Rate of Interest: "+b1.getRateOfInterest());  
System.out.println("ICICI Rate of Interest: "+b2.getRateOfInterest());  
System.out.println("AXIS Rate of Interest: "+b3.getRateOfInterest());  
}  
}
However java object data members ( data member = Unique data for an object ) are not be able to override.

Comments

Popular Posts