ADVERTISEMENT
ADVERTISEMENT

Example of Method Overriding (Run Time Polymorphism) in Java.


IncludeHelp 22 June 2016

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

ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.