Java Overriding

In this post, we will learn the OOP of Java overriding means to override the functionality of an existing method.

A subclass can define a behavior that’s specific to the subclass type, meaning that a subclass can implement a parent class method based on its requirement. This is called Overriding.

Example:

class Animal {
    public void move() {
        System.out.println("Animals can move");
    }
}

class Cat extends Animal {
    public void move() {
        System.out.println("Cats can walk and run");
    }
}

public class TestCat {

    public static void main(String args[]) {
        Animal a = new Animal();   // Animal reference and object
        Animal b = new Cat();   // Animal reference but Cat object

        a.move();   // runs the method in Animal class
        b.move();   // runs the method in Cat class
    }
}

Output

Animals can move
Cats can walk and run
class Animal {
    public void makeSound() {
        System.out.println("Grr...");
    }
}
class Cat extends Animal {
    public void makeSound() {
        System.out.println("Meow");
    }
}

Cat class overrides the makeSound() method of its superclass Animal However, in the runtime, JVM figures out the object type and would run the method that belongs to that particular object.

Example:

class Animal {
    public void move() {
        System.out.println("Animals can move");
    }
}

class Cat extends Animal {
    public void move() {
        System.out.println("Cat can walk and run");
    }
    public void mau() {
        System.out.println("Cat can mau");
    }
}

public class TestCat {

    public static void main(String args[]) {
        Animal a = new Animal();   // Animal reference and object
        Animal b = new Cat();   // Animal reference but Cat object

        a.move();   // runs the method in Animal class
        b.move();   // runs the method in Cat class
        b.mau();
    }
}

Output

/TestCat.java:24: error: cannot find symbol
        b.mau();
         ^
  symbol:   method mau()
  location: variable b of type Animal
1 error

This program will throw a compile time error since b’s reference type Animal doesn’t have a method by the name of mau.

Rules for Method Overriding

  • The argument list should be exactly the same as that of the overridden method.
  • The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass.
  • A method declared final or static cannot be overridden.
  • If a method cannot be inherited, it cannot be overridden.
  • Should have the same return type and arguments.
  • The access level cannot be more restrictive than the overridden method’s access level. For example: If the superclass method is declared public then the overridding method in the sub class cannot be either private or protected.
  • Instance methods can be overridden only if they are inherited by the subclass.
  • A method declared final cannot be overridden.
  • A method declared static cannot be overridden but can be re-declared.
  • If a method cannot be inherited, then it cannot be overridden.
  • A subclass within the same package as the instance’s superclass can override any superclass method that is not declared private or final.
  • A subclass in a different package can only override the non-final methods declared public or protected.
  • An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method.
  • Constructors cannot be overridden.

Using the super Keyword

By using superclass version of an overridden method the super keyword is used.

class Animal {
   public void move() {
      System.out.println("Animals can move");
   }
}

class Cat extends Animal {
   public void move() {
      super.move();   // invokes the super class method
      System.out.println("Cat can walk and run");
   }
}
public class TestCat{
    public static void main(String arg[]){
        Animal b = new Cat(); // Animal reference but Cat Object
        b.move(); //run the method in Cat class
    }
}

Output

Animals can move
Cat can walk and run
Related Post