Introduction
In Java, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, the same parameters or signature, and the same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
Ex:;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | // Java program to demonstrate // method overriding in java // Base Class class Parent { void show() { System.out.println("Parent's show()"); } } // Inherited class class Child extends Parent { // This method overrides show() of Parent @Override void show() { System.out.println("Child's show()"); } } // Driver class public class Main { public static void main(String[] args) { // If a Parent type reference refers // to a Parent object, then Parent's // show is called Parent obj1 = new Parent(); obj1.show(); // If a Parent type reference refers // to a Child object Child's show() // is called. This is called RUN TIME // POLYMORPHISM. Parent obj2 = new Child(); obj2.show(); } } |
Output:
1 2 | Parent's show() Child's show() |
Usage of Method Overriding
- Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
- Method overriding is used for runtime polymorphism.
Rules for Method Overriding
Overriding and Access Modifiers
The access modifier for an overriding method can allow more, but not less, access than the overridden method. For example, a protected instance method in the superclass can be made public, but not private, in the subclass. Doing so will generate a compile-time error.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | // A Simple Java program to demonstrate // Overriding and Access-Modifiers class Parent { // private methods are not overridden private void m1() { System.out.println("From parent m1()"); } protected void m2() { System.out.println("From parent m2()"); } } class Child extends Parent { // new m1() method // unique to Child class private void m1() { System.out.println("From child m1()"); } // overriding method // with more accessibility @Override public void m2() { System.out.println("From child m2()"); } } // Driver class public class Main { public static void main(String[] args) { Parent obj1 = new Parent(); obj1.m2(); Parent obj2 = new Child(); obj2.m2(); } } |
Static methods can not be overridden
When you define a static method with the same signature as a static method in the base class, it is known as method hiding.
Superclass Instance Method | Superclass Static Method | |
---|---|---|
Subclass Instance Method | Overrides | Generates a compile-time error |
Subclass Static Method | Generates a compile-time error | Hides |
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | // Java program to show that // if the static method is redefined by // a derived class, then it is not // overriding, it is hiding class Parent { // Static method in base class // which will be hidden in subclass static void m1() { System.out.println("From parent " + "static m1()"); } // Non-static method which will // be overridden in derived class void m2() { System.out.println( "From parent " + "non - static(instance) m2() "); } } class Child extends Parent { // This method hides m1() in Parent static void m1() { System.out.println("From child static m1()"); } // This method overrides m2() in Parent @Override public void m2() { System.out.println( "From child " + "non - static(instance) m2() "); } } // Driver class class Main { public static void main(String[] args) { Parent obj1 = new Child(); // As per overriding rules this // should call to class Child static // overridden method. Since static // method can not be overridden, it // calls Parent's m1() obj1.m1(); // Here overriding works // and Child's m2() is called obj1.m2(); } } |
Output:
1 2 | From parent static m1() From child non - static(instance) m2() |
Private methods can not be overridden
Private methods cannot be overridden as they are bonded during compile time. Therefore we can’t even override private methods in a subclass.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | class SuperClass { private void privateMethod() { System.out.println( "This is a private method in SuperClass"); } public void publicMethod() { System.out.println( "This is a public method in SuperClass"); privateMethod(); } } class SubClass extends SuperClass { // This is a new method with the same name as the // private method in SuperClass private void privateMethod() { System.out.println( "This is a private method in SubClass"); } // This method overrides the public method in SuperClass public void publicMethod() { System.out.println( "This is a public method in SubClass"); privateMethod(); // calls the private method in // SubClass, not SuperClass } } public class Test { public static void main(String[] args) { SuperClass obj1 = new SuperClass(); obj1.publicMethod(); // calls the public method in // SuperClass SubClass obj2 = new SubClass(); obj2.publicMethod(); // calls the overridden public // method in SubClass } } |
Output:
1 2 3 4 | This is a public method in SuperClass This is a private method in SuperClass This is a public method in SubClass This is a private method in SubClass |
The overriding method must have the same return type
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class SuperClass { public Object method() { System.out.println( "This is the method in SuperClass"); return new Object(); } } class SubClass extends SuperClass { public String method() { System.out.println( "This is the method in SubClass"); return "Hello, World!"; } } public class Test { public static void main(String[] args) { SuperClass obj1 = new SuperClass(); obj1.method(); SubClass obj2 = new SubClass(); obj2.method(); } } |
Output:
1 2 | This is the method in SuperClass This is the method in SubClass |
Invoking overridden method from sub-class
We can call the parent class method in the overriding method using the super keyword.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // A Java program to demonstrate that overridden // method can be called from sub-class // Base Class class Parent { void show() { System.out.println("Parent's show()"); } } // Inherited class class Child extends Parent { // This method overrides show() of Parent @Override void show() { super.show(); System.out.println("Child's show()"); } } // Driver class class Main { public static void main(String[] args) { Parent obj = new Child(); obj.show(); } } |
Output:
1 2 | Parent's show() Child's show() |