Example of Method Overriding (Run Time Polymorphism) in Java.
This code snippet will be used to achieve Run Time Polymorphism using Method Overriding. It is used to provide specific implementation of method that is already declared in super class.
Java Code Snippet - Run Time Polymorphism using Method Overriding
//supar class
class CAR{
public void displayCarName(){
;
}
}
class HondaCity extends CAR{
public void displayCarName(){
System.out.println("Car - Honda City");
}
}
class Ford extends CAR{
public void displayCarName(){
System.out.println("Car - Ford");
}
}
public class ExMthodOverriding{
public static void main(String args[]){
CAR CR1=new HondaCity();
CAR CR2=new Ford();
CR1.displayCarName();
CR2.displayCarName();
}
}
Car - Honda City
Car - Ford