×
☰ See All Chapters

Method Overriding in Java

Having method with same name, same return type, same parameter list declared in subclass as declared in super class is known as method overriding. Only instance methods and inherited methods can be overridden. private, static and final methods cannot be overridden. static method cannot be overridden.  static method is will be at class level but instance methods will be at  object level. private methods cannot be overridden because they are not inherited. final methods cannot be overridden because if a method is marked as final it cannot be overridden. final stops overriding or modification of a method. In java there is no concept of variable overriding and constructor overriding. If the super class method is having an access specifier then the overridden method of the sub class should have an equal or higher access specifier.

 Access specifier in super class

Access specifier which can be used for overridden method of subclass in same package

Private

Cannot override.

default

default, protected, public

protected

protected, public

public

Public

A super class reference variable can refer to subclass object but what members can be accessed from the sub class object depends upon the type of super class reference variable. But if the methods are overridden it always depends on the type of subclass object, the super class reference variable is referring to. This concept is only as dynamic polymorphism. A subclass within the same package of superclass can override any superclass method that is not declared private or final in the same package. A subclass in a different package can only override the non-final methods and which are declared protected or public. The method with default access cannot be overridden because they are not inherited. If the super class method is throwing an exception then the overridden method in the subclass can do any one   of the following (Exceptions will be studied in later chapters)

    1. The overridden method can omit the exception. 

    2. The overridden method can throw the same method level exception as used with the super class method. 

    3. The overridden method can throw any unchecked exceptions, regardless of whether the super class method throws an exception or not. 

The overriding method cannot throw checked exceptions that are new or broader than the ones declared by the super class method. The overriding method can throw narrower or lesser checked exceptions than the overridden method. For example if a method throws IOException then the subclass overridden method cannot throw SQLException (because new type of exception) or Exception (super class type of IOException). But it can throw its subclass type of exception like FileNotFoundException.

Method overriding Example

class A {

        int i, j;

 

        A(int a, int b) {

                i = a;

                j = b;

        }

 

        void show() {

                System.out.println("Inside show method of A");

                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() { // this overrides show() in A

                System.out.println("Inside show method of B");

                System.out.println("k: " + k);

        }

}

 

class Override {

        public static void main(String args[]) {

                B subOb = new B(1, 2, 3);

                subOb.show(); // this calls show() in B

        }

}

Output:

Inside show method of B

k: 3

 

If we wish to access the superclass version of an overridden method, 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.

class B extends A {

        int k;

 

        B(int a, int b, int c) {

                super(a, b);

                k = c;

        }

 

        // display k – this overrides show() in A

        void show() {

                super.show(); // this calls A's show()

                System.out.println("Inside show method of B");

                System.out.println("k: " + k);

        }

}

 

Output:

Inside show method of A

i and j: 1 2

Inside show method of B

k: 3

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:

class A {

        int i, j;

 

        A(int a, int b) {

                i = a;

                j = b;

        }

 

        // display i and j

        void show() {

                System.out.println("Inside show method of A");

                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;

        }

 

        // overloads show()

        void show(String msg) {

                System.out.println(msg + k);

        }

}

 

class Override {

        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(); // this calls show() in A

        }

}

 


All Chapters
Author