When a sub class and super class have methods with same signature,always sub class method is invoked, this process is known as " Overriding " .
Overriding
Oct 12, 2013
Make sure to subscribe to our newsletter and be the first to know the news.
Oct 12, 2013
Make sure to subscribe to our newsletter and be the first to know the news.
When a sub class and super class have methods with same signature,always sub class method is invoked, this process is known as " Overriding " .
Overriding takes place only when inheritance occurs.
Consider the following classes
Car.java (Super Class)
public class Car
{
public void start()
{
System.out.println("start of Car");
}
public void stop()
{
System.out.println("stop of Car");
}
public void horn()
{
System.out.println("Horn of Car");
}
}
HondaCity.java (Sub Class)
public class HondaCity extends Car
{
public void start()
{
System.out.println("start of Honda City");
}
public void stop()
{
System.out.println("stop of Honda City");
}
public void brakes()
{
System.out.println("Brakes of Honda City");
}
}
Now let us try to call the methods..
Demo.java
class Demo
{
public static void main(String[] args)
{
HondaCity city=new HondaCity();
city.start();
city.horn();
city.brakes();
city.stop();
}
}
public void start()
{
super.start();
System.out.println("Start of Honda City");
}