DEV Community

Cover image for Method Overriding In Java
Rahul Raj
Rahul Raj

Posted on

Method Overriding In Java

What is Method Overriding ?

  • Method overriding is a technique in which when child class provides its own implementation/body of a method that is already define in its parent class .
  • Overridden method → Parent class method

  • Overriding method → Child class method

  • For method overriding, the inheritance concept is compulsory.


class Main {
    public static void main(String[] args) {
      Parent p1 = new Parent();
      p1.display();
      Child c1 = new Child();
      c1.display();
      Parent p2 = new Child();
      p2.display();

       // Child c2 = new Parent();

    }
}

class Parent {
    public void display(){
        System.out.println("This is parent class ");
    }
} 

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

}
Enter fullscreen mode Exit fullscreen mode

Output

This is parent class 
This is Child class 
This is Child class 
Enter fullscreen mode Exit fullscreen mode

Rules for defining an overriding method:

  • Rule 1 : Method name must be Same .

  • Rule 2 : Method Parameter Must be same .

  • Note : Rule 1 & Rule 2- Together we can say method signature must be same .

  • Rule 3 : We can not assign More restrictive Access Modifier for overriding method (method in child class )

class Test {
    public static void main(String[] args) {
      Parent p1 = new Parent();
      p1.display();
      Child c1 = new Child();
      c1.display();
      Parent p2 = new Child();
      p2.display();
    //   Child c2 = new Parent();

    }
}

class Parent {
    public void display(){
        System.out.println("This is parent class ");
    }
} 

class Child extends Parent {
    protected void display(){ // error 
        System.out.println("This is Child class ");
    }

}

Enter fullscreen mode Exit fullscreen mode

Output :

error: display() in Child cannot override display() in Parent
    protected void display(){
                   ^
  attempting to assign weaker access privileges; was public
1 error
ERROR!
error: compilation failed
Enter fullscreen mode Exit fullscreen mode
  • Note : private(more restrictive access modifier ) → default → protected → public (less restrictive access modifier )

  • Rule 4 : for return type

a>. Up to Java 1.4 version, the return type must be exactly the same.
b>. From Java 1.5 onwards, for primitive return types : the return type must be the same, but for object return types , covariant return types are allowed.


class Test {
    public static void main(String[] args) {
      Parent p1 = new Parent();
     System.out.println("Parent Class Method "+p1.display());
      Child c1 = new Child();
      System.out.println("Child Class Method "+c1.display());
      Parent p2 = new Child();
     System.out.println("Child Class Method "+p2.display());


    }
}

class Parent {
    public int display(){
        return 10;
    }
} 

class Child extends Parent {
    public int display(){
        return 20;
    }

}

Enter fullscreen mode Exit fullscreen mode

Output

Parent Class Method 10
Child Class Method 20
Child Class Method 20
Enter fullscreen mode Exit fullscreen mode
  • note : Covariant return type for Object only not primitive

class Test {
    public static void main(String[] args) {
      Parent p1 = new Parent();
     System.out.println("Parent Class Method "+p1.display());
      Child c1 = new Child();
      System.out.println("Child Class Method "+c1.display());
      Parent p2 = new Child();
     System.out.println("Child Class Method "+p2.display());


    }
}

class Parent {
    public Object display(){
        return 10;
    }
} 

class Child extends Parent {
    public String display(){
        return "20";
    }

}

Enter fullscreen mode Exit fullscreen mode

Output

Parent Class Method 10
Child Class Method 20
Child Class Method 20
Enter fullscreen mode Exit fullscreen mode

Another Example of Covariant Return Type :


class Test {
    public static void main(String[] args) {
      Parent p1 = new Parent();
     System.out.println("Parent Class Method "+p1.display());
      Child c1 = new Child();
      System.out.println("Child Class Method "+c1.display());
      Parent p2 = new Child();
     System.out.println("Child Class Method "+p2.display());


    }
}

class Parent {
    public  Number  display(){
        return 10;
    }
} 

class Child extends Parent {
    public Integer display(){
        return 20;
    }

}

Enter fullscreen mode Exit fullscreen mode
  • Rule 5 : Rules for defining a method as final a>If a parent class method is final, then we cannot override that method in the child class; otherwise, we get a compile-time error. b> We may define a child class method as final; there is no problem.

class Test {
    public static void main(String[] args) {
      Parent p1 = new Parent();
     System.out.println("Parent Class Method "+p1.display());
      Child c1 = new Child();
      System.out.println("Child Class Method "+c1.display());
      Parent p2 = new Child();
     System.out.println("Child Class Method "+p2.display());


    }
}

class Parent {
    public Object display(){
        return 10;
    }
} 

class Child extends Parent {
    public final String display(){
        return "20";
    }

}

Enter fullscreen mode Exit fullscreen mode

Output :

Parent Class Method 10
Child Class Method 20
Child Class Method 20
Enter fullscreen mode Exit fullscreen mode
  • Rule 6 : We can define both parent class and child class methods as private; there is no issue.
class Test {
    public static void main(String[] args) {
      //Parent p1 = new Parent();
     //p1.display();
      Child c1 = new Child();
      c1.display();
      //Parent p2 = new Child();
     //p2.display();


    }
}

class Parent {
     private  void  display(){
       System.out.println("parent class private method ");
    }

} 

class Child extends Parent {
    public  void display(){
       System.out.println("child class non - private display  method ");
    }

}

Enter fullscreen mode Exit fullscreen mode

OutPut :

child class non - private display  method 
Enter fullscreen mode Exit fullscreen mode
  • Rule 7 : If the parent class method is static but the child class method is non-static, it is not allowed to override. But , We can define a method as static in both parent and child classes.(method hiding)

class Test {
    public static void main(String[] args) {
      Parent p1 = new Parent();
     p1.display();
      Child c1 = new Child();
      c1.display();
      Parent p2 = new Child();
     p2.display();


    }
}

class Parent {
     public static  void  display(){
       System.out.println("parent class private method ");
    }

} 

class Child extends Parent {
      void display(){
       System.out.println("child class non - private display  method ");
    }

}


Enter fullscreen mode Exit fullscreen mode

output

 error: display() in Child cannot override display() in Parent
      void display(){
           ^
  overridden method is static
1 error
Enter fullscreen mode Exit fullscreen mode

class Test {
    public static void main(String[] args) {
      Parent p1 = new Parent();
     p1.display();
      Child c1 = new Child();
      c1.display();
      Parent p2 = new Child();
     p2.display();


    }
}

class Parent {
     public static  void  display(){
       System.out.println("parent class private method ");
    }

} 

class Child extends Parent {
     public static void display(){
       System.out.println("child class non - private display  method ");
    }

}

Enter fullscreen mode Exit fullscreen mode
parent class private method
child class non - private display  method
parent class private method
Enter fullscreen mode Exit fullscreen mode

class Test {
    public static void main(String[] args) {
      Parent p1 = new Parent();
     p1.display();



    }
}

class Parent {
     public   void  display(){
       System.out.println("parent class private method ");
    }

} 

 abstract class Child extends Parent {
     public abstract void display();

}

Enter fullscreen mode Exit fullscreen mode
parent class private method
Enter fullscreen mode Exit fullscreen mode
class Test {
    public static void main(String[] args) {
      //Parent p1 = new Parent();
     //p1.display();




    }
}

abstract class Parent {
     public  abstract void  display();

} 

 abstract class Child extends Parent {
     public abstract void display();

}

Enter fullscreen mode Exit fullscreen mode
//output ->  blank page 

Enter fullscreen mode Exit fullscreen mode

overriding

ovrriding

Top comments (0)