Java Method Overriding

In a class hierarchy, when method in a subclass having the same name and type signature as method in its superclass, then the method present in subclass is said to override the method in superclass.

When an overridden method is called from inside its subclass, it will always refer to the version of that method defined by the subclass. The version of the method is defined by the superclass will be hidden. Consider the following example:

Java Method Overriding Example

Here is an example demonstrates method overriding in Java:

/* Java Program Example - Java Method Overriding
 * This program demonstrates the Method Overriding */
 
 
 class A
 {
     int i, j;
     
     A(int x, int y)
     {
         i = x;
         j = y;
     }
     
     /* prints the value of i and j */
     void display()
     {
         System.out.println("i and j : " + i + "  " + j);
     }
 }
 
 class B extends A
 {
     int k;
     
     B(int x, int y, int z)
     {
         super(x, y);
         k = z;
     }
     
     /* print k's value, this overrides the display() in A */
     void display()
     {
         System.out.println("k : " + k);
     }
 }
 
 class JavaProgram
 {
     public static void main(String args[])
     {
    
         B subObj = new B(10, 20, 30);
         
         subObj.display();     // this calls the display() in B
       
     }
 }

When the above Java program is compile and executed, it will produce the following output:

java method overriding

When the display() is invoked on an object of the type B, the version of the display() defined within B is used i.e., the version of the display() method inside the B overrides the version declared in the A.

If you wish to access the superclass version of an overridden method, you can do so by using the super. For example, in this version of B, the superclass version of the display() is invoked inside the subclass version. This allows all the instance variables to be displayed.

/* Java Program Example - Java Method Overriding
 * This program demonstrates Method Overriding */
 
 
 class A
 {
     int i, j;
     
     A(int x, int y)
     {
         i = x;
         j = y;
     }
     
     /* print i and j */
     void display()
     {
         System.out.println("i and j : " + i + "  " + j);
     }
 }
 
 class B extends A
 {
     int k;
     
     B(int x, int y, int z)
     {
         super(x, y);
         k = z;
     }
     
     /* print k, this overrides the display() in A */
     void display()
     {
         super.display();              // this calls A's display()
         System.out.println("k : " + k);
     }
 }
 
 class JavaProgram
 {
     public static void main(String args[])
     {
    
         B subObj = new B(10, 20, 30);
         
         subObj.display();     // this calls the display() in B
       
     }
 }

When the above Java Program is compile and executed, it will produce the following output:

method overriding in java

Here, super.display() calls the superclass version of the display().

Method overriding occurs only when the names and the type signatures of the two methods are same. If they aren't, then the two methods are simply overloaded. For example, consider the following example program which is the modified version of the preceding example :

/* Java Program Example - Java Method Overriding
 * Methods with differing type signatures are 
 * overloaded, not overridden
 */
 
 
 class A
 {
     int i, j;
     
     A(int x, int y)
     {
         i = x;
         j = y;
     }
     
     /* print i and j */
     void display()
     {
         System.out.println("i and j : " + i + "  " + j);
     }
 }
 
 /* now create a subclass by extending class A */
 class B extends A
 {
     int k;
     
     B(int x, int y, int z)
     {
         super(x, y);
         k = z;
     }
     
     /* overload display() */
     void display(String msg)
     {
         System.out.println(msg + k);
     }
 }
 
 class JavaProgram
 {
     public static void main(String args[])
     {
    
         B subObj = new B(10, 20, 30);
         
         subObj.display("This is k : ");     // calls display() in B
         subObj.display();                   // calls display() in A
       
     }
 }

When the above Java program is compile and executed, it will produce the following output:

method overriding in java

The version of the display() in B takes a string parameter. This makes its type signature different from the one in A which takes no parameters. Thus, no overriding (or name hiding) takes place. Instead, the version of the display() in B simply overloads the version of the display() in A.

Java Online Test


« Previous Tutorial Next Tutorial »