1
0

Method Overriding in Java

We have seen that a method defined in a super class is inherited by its subclass and is used by the objects created by the subclass. Method inheritance enables us to define and use methods repeatedly in a subclass without having to define the methods again in the subclass. However, there may be occasions when we want an object to respond to the same method but have different behavior when that method is called. That means we should override the method defined in the superclass. This is possible by defining a method in the subclass that has the same name, same arguments, and same return type as a method in the superclass. Then, when that method is called, the method defined in the subclass is invoked and executed instead of the one in the superclass. This is known as method overriding. The following program illustrates the concept of overriding.

Program to demonstrate Method overriding.

class A
{
    int i, j;
    A(int a, int b)
    {
        i = a;
        j = b;
    }
    void show()     // display i and j
    {
        System.out.println("i and j: " + i + " " + j);
    }
}
class B extends A
{
    int k;
    B(int a, int b, int c)
    {
        super(a, b);
        k = c;
    }
    void show()
    {
        System.out.println("k: " + k);
    }
}
class Override
{
    public static void main(String s[])
    {
        B subOb = new B(1, 2, 3);
        subOb.show();   // this calls show() in B
    }
}

The output produced by this program is shown here:

k: 3

When show() is invoked on an object of type B, the version of show() defined within B is used. That is, the version of show() inside B overrides the version declared in A.

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

Program to demonstrate the invocation of the overridden method

class B extends A
{
    int k;
    B(int a, int b, int c)
    {
        super(a, b);
        k = c;
    }
    void show()
    {
        super.show();   // this calls A's show()
        System.out.println("k: " + k);
    }
}

If you substitute this version of class B into the previous program, you will see the following output:

i and j: 1 2
k: 3

Here, super.show() calls the superclass version of show(). Method overriding occurs only when the names and the type signatures of the two methods are identical. If they are not, then the two methods are simply overloaded. For example, consider this modified version of the preceding example:

Program to show that Methods with differing type signatures are overloaded – not overridden.

class A
{
    int i, j;
    A(int a, int b)
    {
        i = a;
        j = b;
    }
    void show()     // display i and j
    {
        System.out.println("i and j: " + i + " " + j);
    }
}
// Create a subclass by extending class A.
class B extends A
{
    int k;
    B(int a, int b, int c)
    {
        super(a, b);
        k = c;
    }
    void show(String msg)   // overload show()
    {
        System.out.println(msg + k);
    }
}
class OverLoad
{
    public static void main(String args[])
    {
        B subOb = new B(1, 2, 3);
        subOb.show("This is k: ");  // this calls show() in B
        subOb.show();
    }
}

The output produced by this program is shown here:

This is k: 3
i and j: 1 2

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

Overriding V/S Overloading

Method overriding should not be confused with method overloading. Method overriding requires the same method signature i.e. name and parameters and the same return type. Only non-final instance methods in the super class that are directly accessible from the sub class are eligible for overriding. Overloading occurs when the method names are the same, but the parameter lists are different. Therefore, to overload methods, the parameters must differ in type, order, or number. As the return type is not a part of the signature, having a different return type is not enough to overload methods.

A method can be overloaded in the class it is defined in or in a sub class of its class. Invoking an overridden method of the super class from a subclass requires special syntax (for example a keyword super). This is not necessary for invoking an overloaded method in the method call occurring in the sub class, the overloaded method in the super class will be invoked.

Leave a Reply