Tutorial: Method Overriding in JAVA – Programming Best Practices

During Inheritance, when a method definition have the same method signature ( method name+ parameter signature) and return type in a subclass as that of its super class, then the method in the subclass is said to override the method of its super class and the mechanism is so called Method Overriding.

Having been created object of the subclass, when the overridden method is called then the object of subclass refers to the method which is defined in the subclass that does mean the overridden method which is defined in the super class will be hidden. In order to access the overridden method defined in the super class can be accessed by the subclass using ‘super’ keyword.

In OOPs, Method Overriding is a feature that let subclass define specific implementation of method which is already defined in one of its super classes’. Among the overridden methods, which version of the method will be called will be determined by the invoking object at run-time. That’s what it is called Run-time Polymorphism as Overridden methods implement “one interface, multiple use” aspect of polymorphism.

In method overriding, Java run-time system resolves the method invocation dynamically at run-time, so called Late Binding.

Features of Method Overriding

  • The access level of the overriding method in subclass can not be more restrictive than the overridden method of the super class.
  • The methods will be said to override one another when there is a Parent- Child relation between the classes.
  • Private and Final methods can not be overridden.
  • Static methods can not be overridden but can be re-declared.
  • Public or Protected non-final methods of a package can only be overridden by its subclass existing in some other different package.
  • Constructors can not be overridden.
  • Overridden can throw any Unchecked Exception.

Advantages

Supports Specialization i.e. during inheritance when a subclass inherits a parent class, then subclass will have all the features of parent class and on the top of that subclass can implement specific functionality to the overriding method having same method signature and return type as same as in its super class.

Java Program

class Animal{
  void sound(){
    System.out.println("default sound...");
  }
}

class Cat extends Animal{
  void sound(){
    System.out.println("mew mew...");
  }
}

class Dog extends Animal{
  void sound(){
    System.out.println("vow vow...");
  }
}

class MethodOverrideDemo{
  public static void main(String args[]){
    Cat cat = new Cat();
    Dog dog = new Dog();
    System.out.println("--------Cat--------");
    cat.sound();
    System.out.println("--------Dog--------");
    dog.sound();
  }
}

Output

method_override

Explanation of Code & Output

In the example sited above, Dog and Cat both are subclasses which are derived from Animal super class. In all the classes (except MethodOverrideDemo class) you will find the method sound (), which is defined under Animal class in a generic way and in Cat and Dog class has overridden the sound () method of Animal class with specific implementation as per scenario. Obviously Cat’s sound is “mew mew…” and that of dog’s “vow vow…” sound () is defined in these classes according to the need. If we create object of class Cat, then it will invoke sound () method of Cat class, same is true for object of Dog class. I guess from the output everything is clear.

Checkout more useful tutorials and definitive guidelines on Java programming here.

About the author

Nitin Agarwal

A blogger, tech evangelist, YouTube creator, books lover, traveler, thinker, and believer of minimalist lifestyle.