When Superclass and subclass have method having same signature and name then a call to that method using subclass object will lead to invoking subclass version of that method. This is called method overriding.
Following Code will illustrate the concept:
Code::
class d
{
void show()
{
System.out.println("D's method");
}
}
class e extends d
{
void show()
{
System.out.println("E's method");
}
}
class f extends e
{
void show()
{
System.out.println("F's method");
}
}
public class MethodOverriding {
public static void main(String[] nt)
{
f ob=new f();
ob.show();
}
}
Output::
As the above example shows that only subclass version is called.
Following Code will illustrate the concept:
Code::
class d
{
void show()
{
System.out.println("D's method");
}
}
class e extends d
{
void show()
{
System.out.println("E's method");
}
}
class f extends e
{
void show()
{
System.out.println("F's method");
}
}
public class MethodOverriding {
public static void main(String[] nt)
{
f ob=new f();
ob.show();
}
}
Output::
As the above example shows that only subclass version is called.