Two functions can define with the same name and prototype, one placed in the super class and another one placed in the subclass. The method of the subclass was written for altering or modifying the task of the super class. This feature of object oriented programming is known as method overriding.
public class Human
{
public void say()
{
System.out.println("It is super class version");
}
}public class Indian extends Human
{
public void say()
{
System.out.println("It is sub class version");
}
}public class TestHumanIndian
{
public static void main(String [] args)
{
Indian i =new Indian();
i.say();
}
}
Note: If the method of the super class declared as final then that method cannot be override in sub class.
Note: If we call an overridden method then it normally invokes the sub class version of the method. A keyword, super is used to invoke the super class version method.
public class Human
{
public void say()
{
System.out.println("It is super class version");
}
}public class Indian extends Human
{
public void say()
{
System.out.println("It is sub class version");
}
void TestCall()
{
say();
super.say();
}
}public class TestHumanIndian
{
public static void main(String [] args)
{
Indian i =new Indian();
i.TestCall();
}
}
If the function or method declared as static or final then that method cannot be overridden. Static methods can redesign but not overridden.
Note: Constructor cannot be overridden.
Note: abstract methods must be overridden at the first non-abstract class.
In overloading both the function placed in the same class. The names of the functions are same but the prototype, specially signature of the methods are different. But in overriding methods are placed different classes, one in super class and another one in sub class. But the name and prototype are the same for the both of the functions.