Adds

Method Overriding in Java

What is Method Overriding?

In java if subclass contains same method as declared in the parent class then it is known as method overriding.Sometimes, the sub class may want to provide a specific implementation of methods defined in the super class, it is referred as method overriding. Method overriding allows a sub class to provide its own implementation of a method already provided by one of its super classes.


Example of method overriding

In this example Dog class extends Animal class. Animal is parent class and it contains run() method. Dog class is overriding run() method and providing its own implementation of run() method.

class Animal{
public void run()
{
System.out.println("Animal is running");
}
}

class Dog extends Animal{
public void run(){
System.out.println("Dog is running");
}
public static void main( String args[]) {
Dog obj = new Dog();
obj.run();
}

Output

Dog is running

Advantage of method overriding

The main advantage of method overriding is that the class can give its own specific implementation to a inherited method without modifying the parent class.


Method Overriding Rules

  • Child class Method name must be same as the parent class.
  • Method must have same argument as in the parent class.The data types of the arguments and their order should be maintained as it is in the overriding method(child class method).
  • A method declared final cannot be overridden.
  • Constructors cannot be overridden.
  • A method declared static cannot be overridden but can be re-declared. We will discuss this in separate topic.
  • Subclass method access specifier must be same as super class method access specifier or higher than super class method specifier For e.g. if the Access specifier of parent class method is public then the overriding method (child class method ) cannot have private, protected and default Access specifier as all of the three are more restrictive than public.

  • Super ClassSubclass
    PrivatePrivate, default ,protected ,public
    defaultdefault ,protected ,public
    protectedprotected ,public
    publicpublic

    Example:


    1 . InValid Override :

    class Animal{
    
    public void run()
    
    {
    
    System.out.println("Animal is running");
    
    }
    
    }
    
    class Dog extends Animal{
    
    protected void run(){
    
    System.out.println("Dog is Running");
    
    }
    
    public static void main( String args[]) {
    
    Dog obj = new Dog();
    
    obj.run();
    
    }
    
    }
    

    Output

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	Cannot reduce the visibility of the inherited method from Animal
    
    	at com.example.Dog.run(Dog.java:16)
    	at com.example.Dog.main(Dog.java:26)
    

    Above code is not a valid override as child class run() method is more restrictive(protected) than base class(public).


    1 . Valid Override :

    class Animal{
    
    protected void run()
    
    {
    
    System.out.println("Animal is running");
    
    }
    
    }
    
    class Dog extends Animal{
    
    public	void run(){
    
    System.out.println("Dog is Running");
    
    }
    
    public static void main( String args[]) {
    
    Dog obj = new Dog();
    
    obj.run();
    
    }
    
    }
    

    Output

    Dog is Running
    

    Above code is perfectly valid scenario as public is less restrictive than protected. Same access modifier is also a valid case of override.

  • Be careful while handling exception in method overriding. If the superclass method does not declare an exception, subclass overridden method cannot declare the checked exception but it can declare unchecked exception.If the superclass method declares an exception, subclass overridden method can declare same, subclass exception or no exception but cannot declare parent of the exception.Which means if overridden method throws IOException than overriding method can not throw java.lang.Exception in its throws clause because java.lang.Exception comes higher than IOException in Exception hierarchy.

Difference between method overloading and Method overriding

SNOMethod OverloadingMethod Overriding
1 In Method Overloading, Methods of the same class shares the same name but each method must have different number of parameters or parameters having different types and order. In Method Overriding, sub class have the same method with same name and exactly the same number and type of parameters and same return type as a super class.
2 Overloading happens at compile-time. Overriding happens at runtime.
3 In Method Overloading, methods must have different signature. In Method Overriding, methods must have same signature.
4 static method can be overloaded. you can not override static method in Java. In fact when you declare same method in Sub Class it's known as method hiding because it hide super class method instead of overriding it.
5 private and final method can be overloaded. private and final method can not be overridden.


Next Article:- Method Hiding





comments powered by Disqus