Method Overriding in Java

Method Overriding in Java

Whatever methods the parent class has by default are available to the child class through inheritance. If the child class is not satisfied with the method implementation available in the parent class then the child class is allowed to redefine that method based on its requirement. this process is known as method overriding.

The parent class method which is overriding is called the overridden method and the child class method which is overriding parent class implementation is called the overriding method.


     class Parent {
    public void greeting(){
        System.out.println("Greeting from parent");
        
     }
 }
 class Child extends Parent{
     @Override
     public void greeting() {
         System.out.println("Greeting from child");
     }
 }

In the overriding method resolution always take care of by JVM based on the runtime object, hence overriding is also considered as runtime polymorphism or dynamic polymorphism late binding.


class Parent {
    public void greeting(){
        System.out.println("Greeting from parent");

     }
 }
 class Child extends Parent{
     @Override
     public void greeting() {
         System.out.println("Greeting from child");
     }
 }
 class TheCodeData {
     public static void main(String[] args) {
         Parent parent = new Parent();
         parent.greeting();
         Child child = new Child();
         child.greeting();
         Parent parentt = new Child();
         parentt.greeting();
     }
 }
Overriding in Java

Rules For Method Overriding

  • In the overriding method names must be the same that is method signature(name and arguments type) must be the same.
  • In overriding return type must be the same but this rule is applicable until Java1.4v, from Java 1.5v onwards we can take a co-variant return type. According to this child class method return type need not be the same as the parent class method return type.
package MethodOverriding;

public class Parent {
    public Object method(){
    return null;    
    }
    
}
class Child extends Parent{
    public String method(){
        return null;
    }
}

Folowing co-varient return type is acceptable

Parent Object -> Object/String/StringBuffer Child

Parent Number -> Number/Integer Child

Co-varient return type concept is only applicable for object types, not for primitive types.

  • Parent class private method is not available to the child class hence overriding concept does not apply to the private method. Based on our requirement we can declare the same method in the child class but it is not method overriding.

public class Parent {
    private void method(){
        System.out.println("Parent class method");
    }
    
}
class Child extends Parent{
    private void method(){
        System.out.println("Child class method");
    }
}
  • We cannot override the parent class final method in the child class, If we try to do so then we will get compile time error-“‘method()’ cannot override ‘method()’ in ‘MethodOverriding.Parent’; overridden method is final”.
package MethodOverriding;

public class Parent {
   public final void  method(){
        System.out.println("Parent class method");
    }

}
class Child extends Parent{
   public void method(){
        System.out.println("Child class method");
    }
}

  • We should override the parent class abstract method in the child class to provide the implementation.
package MethodOverriding;

abstract class Parent {
   public abstract void  method();
}
class Child extends Parent{
   public void method(){
        System.out.println("Child class method");
    }
}
  • We can override the nonabstract method as abstract. The main advantage of this is we can stop the availability of parent class method to next level child class.
package MethodOverriding;

public class Parent {
   public  void  method(){
       System.out.println("Parent class method");
   }
}
abstract class Child extends Parent{
   public abstract void method();
}

Parent method modifier and Child method modifier rule

Parent method modifierchild method modifiervalid/invalid
finalnon-final/finalInvalid
non-finalfinalValid
abstractnon-abstractValid
synchronizednon-synchronizedValid
nativenon-nativeValid
strictfpnon-strictfpValid

In overriding there is no restriction on following modifiers –

  • synchronized
  • native
  • strictfp

Rules for Modifiers in Overriding

  • While overriding we can not reduce the scope of the access modifier but we can increase the scope.
package MethodOverriding;

public class Parent {
    public static void method(){
        System.out.println("Parent method");
    }
   }
class Child extends Parent{
    public static void method(int x){
        System.out.println("Child method");
    }
}
class TheCodeData{
    public static void main(String[] args) {
        Parent p = new Parent();
        p.method();
        Child c = new Child();
        c.method();
    }
}
Rules for Modifiers in Overriding

  • We cannot override a static method as a nonstatic method otherwise we will get compile time error.
  • We cannot override a nonstatic method as static. otherwise, we will get compile time error.
  • If both parent and child class methods are static then we will not get any compile time error but this is not overriding it is method hiding.

Method Hiding

All rules of method hiding are exactly same as overriding except the following diffrences.

Method HidingMethod Overriding
Both methods should be static Both method should be non-static
The compiler is responsible for method resolution based on reference typeJVM is responsible for method resolution based on runtime objects.
It is also known as compile-time polymorphism, static polymorphism, or early bindingIt is also known as runtime polymorphism, dynamic polymorphism, or late polymorphism

Overriding with respect to var-args method

We can override the var-arg method with another var-args method only if we are trying to override with the normal method, then it will become overloading but not overriding.

package MethodOverriding;

public class Parent {
    public void method(int... x){
        System.out.println("Parent method");
    }
   }
class Child extends Parent{
    public void method(int x){
        System.out.println("Child method");
    }
}
class TheCodeData{
    public static void main(String[] args) {
        Parent p = new Parent();
        p.method(45);
        Child c = new Child();
        c.method(67);
        Parent p1 = new Child();
        p1.method(56);
    }
}
Overriding with respect to var-args method

In the above Java program if we replace the child method with the var-arg method then it will become overloaded.

Overriding with respect to variables

Variables resolution always takes care of by the compiler based on reference type irrespective of whether the variable is static or nonstatic (The overriding concept is only applicable for methods not for variables).

package MethodOverriding;

public class Parent {
    int var = 111;
   }
class Child extends Parent{
    int var = 222;
}
class TheCodeData{
    public static void main(String[] args) {
        Parent p = new Parent();
        System.out.println(p.var);
        Child C = new Child();
        System.out.println(C.var);
        Parent p1 = new Child();
        System.out.println(p1.var);
    }
}
Overriding with respect to variables

Similar Java Tutorials

  1. Bitwise operator in Java
  2. Operators in Java
  3. Public static void main
  4. Command line argument in Java
  5. Java coding standards
  6. Control Flow Statements in Java
  7. If – else in Java
  8. Switch statements in Java
  9. For loop in Java
  10. While loop in Java
  11. Do while loop in Java
  12. Import statement in Java
  13. Abstract modifier in Java
  14. Strictfp modifier in Java
  15. Final variable in Java
  16. Static modifier in Java
  17. Native modifier in Java
  18. Transient keyword in Java
  19. Volatile keyword in Java
  20. Adapter Class in Java
  21. Method signature in Java
  22. Method overloading in Java
  23. Has A Relationship
  24. Inheritance in Java
  25. Encapsulation in Java
  26. Abstraction in Java
  27. Data Hiding in Java
  28. Interface vs abstract class

3 thoughts on “Method Overriding in Java”

Leave a Comment