Overriding in Java

Overring in Java is an example of runtime polymorphism.

Implementing a method in sub class which is already present in parent class is known as method overriding.

In other words, 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.

Method overriding in Java programming occurs when the method in the subclass has the same return type, or parameters, name or signature as the parent class.

The advantages of overriding

  1. Ability to use same behavior of parent class into child class.
  2. Ability to override and change a behavior of parent class in subclass based on its requirement.

Rules for Method Overriding

  1. The method name and arguments should be exactly the same in parent class and child class.
  2. The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass.
  3. A method declared final cannot be overridden.
  4. The access level cannot be more restrictive than the overridden method's access level.
  5. A method declared static cannot be overridden but can be re-declared.
  6. We can't override Constructors.

Example of overriding

Lets take a simple example to understand overring. We have two classes: A Child class and a Parent class. The Child class extends Parent class. Both the classes have a common method void eat(). Child and Parent both have different way to eat food So, Child class is giving its own implementation to the eat() method or in other words it is overriding the eat() method.

package com.jepo;

class Parent{
   //parent method
   public void eat()
      System.out.println("Parent is eating");
public class Boy extends Parent {
   //Overriding parent method
   public void eat(){
      System.out.println("Child is eating");
   public static void main( String args[]) {
      Boy obj = new Boy();
      //call the child class method of eat()

Output: Child is eating

Super keyword in Method Overriding

The super keyword is used for calling the parent class method/constructor.


package com.db;

class Parent{
   public void show()
    System.out.println("Show method of parent class");
class Child extends Parent{
   public void show(){
    //This will call the show() of parent class
    System.out.println("Show method of child class");
   public static void main( String args[]) {
    Child obj = new Child();


Show method of parent class
Show method of child class

Java Access-Modifiers rules in overriding



Can Static Methods overridden

"No". Static methods cannot be overridden because static methods is associated with class not the objects.

Static methods loaded at the class loading time but overriding is done at runtime.

Final Methods cannot be Overridden Methods in Java

Overriding Method must have the same Return Type or Subtype

Private Methods cannot be Overridden Method in Java

Overriding and Java Constructor

Invoking Overridden Method from Sub-Class

Overriding and Abstract Method in Java

Overriding and Synchronized/Stricfp Method in Java

Overriding and Exception-Handling in Java

Method Overriding vs. Overloading in Java

 Method Overloading  Method Overriding
 Parameter must be different and name must be same.  Both name and parameter must be same.
 Increase readability of code.  Increase reusability of code.
 Compile time polymorphism.  Runtime polymorphism.
 Access specifier can be changed.  Access specifier cannot be more restrictive than original method(can be less restrictive).