What is Method Overriding in Java

When a Sub class has the implementation of the same method which is defined in the Parent class then it is called as Method Overriding. Unlike Method Overloading in Java the parameters passed will not differ in Overriding. Lets now look in how to Override a method in Java.

Lets take the below example we have two classes Parent and Child, where Child extends Parent.

Parent.java

package com.javainterviewpoint;

public class Parent 
{
    public void display(String name)
    {
        System.out.println("Welcome to Parent Class \""+name+"\"");
    }
    public void disp()
    {
        System.out.println("disp() method of Parent class");
    }
}

In the Parent class we have two method display() and disp() nothing else.

Child.java

package com.javainterviewpoint;

public class Child extends Parent
{
    public void display(String name)
    {
        System.out.println("Welcome to Child Class \""+name+"\"");
    }
    public void show()
    {
        System.out.println("show() method of Child class");
    }
    public static void main(String args[])
    {
        //Create object for the parent class 
        Parent p = new Parent();
        //Calling parent class methods
        p.display("JavaInterviewPoint");
        p.disp();
        
        //Creating object for the child class
        Child c = new Child();
        c.display("JavaInterviewPoint");
        c.show();
    }
}

Child class extends the Parent class and overrides the display() method and has its own method show(). In the main() method we will be creating objects for both Parent and Child class and their individual methods are called.
When we run the above code we will get the below output

Welcome to Parent Class "JavaInterviewPoint"
disp() method of Parent class
Welcome to Child Class "JavaInterviewPoint"
show() method of Child class

The above example represents the simple Overriding technique where we create the objects for individual class and call the correspoding methods.

Role of Access Modifiers in Overriding

The access modifier of the overriding method(method in the Child class) cannot be more restrictive than the Parent class. Lets take below example where we have the display() method with access modifier as “public” in Parent class and the Child class cannot have “private” or “protected” or “default” modifiers as all of them are more restrictive than “public”

class Parent 
{
    public void display(String name)
    {
        System.out.println("Welcome to Parent Class \""+name+"\"");
    }
}
public class Child extends Parent
{
    private void display(String name)
    {
        System.out.println("Welcome to Child class \""+name+"\"");
    }
    public static void main(String args[])
    {
        //Create object for Child class
        Child c = new Child();
        c.display("JIP");
        
        //Create object for Parent class will work here
        Parent p = new Parent();
        p.display("JIP");
     }
}

when you run the above code then you will get compilation error “Cannot reduce the visibility of the inherited method from Parent”. But when the Parent is more restrictive than Child class then it is allowed, lets see that too

package com.javainterviewpoint;

class Parent 
{
    protected void display(String name)
    {
        System.out.println("Welcome to Parent Class \""+name+"\"");
    }
}
public class Child extends Parent
{
    public void display(String name)
    {
        System.out.println("Welcome to Child class \""+name+"\"");
    }
    public static void main(String args[])
    {
        //Create object for Child class
        Child c = new Child();
        c.display("JIP");
        
        //Create object for Parent class
        Parent p  = new Parent();
        p.display("JIP");
     }
}

The above code runs fine without any exception as the Child class method is less restrictive than the Parent class method.
output :

Welcome to Child class "JIP"
Welcome to Parent Class "JIP"

Use of Super keyword in Overriding

We can use the super keyword to call the Parent class method inside the Child class method. In the below code we have called the Parent class display() method from Child class.

package com.javainterviewpoint;

class Parent 
{
    public void display(String name)
    {
        System.out.println("Welcome to Parent Class \""+name+"\"");
    }
}
public class Child extends Parent
{
    public void display(String name)
    {
        System.out.println("Welcome to Child class \""+name+"\"");
        super.display("JIP");
    }
    public static void main(String args[])
    {
        //Create object for Child class
        Child c = new Child();
        c.display("JIP");
     }
}

Exception Handling in Overriding

Below are the rules which has to be followed when go for Method Overriding with Exception Handling.

  1. When Parent class method doesn’t throw any Exception then Child class overriden method also cannot declare any Checked Exception (Compile time Exception).
  2. When Parent class method doesn’t throw any exception then Child class overriden method can declare UnChecked Exception (Runtime Exception).
  3. When Parent class declares an Exception then the Child class overriden method can declare the same or sub class exception or no exception.
  4. When Parent class declares an Exception then the Child class overriden method  cannot declare super class exception

1. When Parent class method doesn’t throw any Exception then Child class overriden method also cannot declare any Checked Exception

package com.javainterviewpoint;

import java.io.IOException;

class Parent 
{
    public void display()
    {
        System.out.println("Welcome to Parent Class");
    }
}
public class Child extends Parent
{
    public void display() throws IOException
    {
        System.out.println("Welcome to Child class");
    }
    public static void main(String args[]) throws IOException
    {
        //Create object for Child class
        Child c = new Child();
        c.display();
        
        //Create object for Parent class
        Parent p = new Parent();
        p.display();
     }
}

Here we have the Parent Class display() method which doesn’t throw any exception and Child class has overriden the display() method and throws IOException. As IOException is a Checked Exception we cannot have it thrown and hence it will give the below exception.

Output :

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Exception IOException is not compatible with throws clause in Parent.display()

	at com.javainterviewpoint.Child.display(Child.java:14)
	at com.javainterviewpoint.Child.main(Child.java:22)

2. When Parent class method doesn’t throw any exception then Child class overriden method can declare UnChecked Exception

package com.javainterviewpoint;

class Parent 
{
    public void display()
    {
        System.out.println("Welcome to Parent Class");
    }
}
public class Child extends Parent
{
    public void display() throws ArrayIndexOutOfBoundsException
    {
        System.out.println("Welcome to Child class");
    }
    public static void main(String args[]) throws ArrayIndexOutOfBoundsException
    {
        //Create object for Child class
        Child c = new Child();
        c.display();
        
        //Create object for Parent class
        Parent p = new Parent();
        p.display();
     }
}

The Parent Class display() method which doesn’t throw any exception and Child class has overriden the display() method and throws ArrayIndexOutOfBoundsException. As ArrayIndexOutOfBoundsException is a UnChecked Exception we can have it thrown and hence it will run without any issue.

Output :

Welcome to Child class
Welcome to Parent Class

3. When Parent class declares an Exception then the Child class overriden method can declare the same or sub class exception or no exception

package com.javainterviewpoint;

class Parent 
{
    public void display() throws ArrayIndexOutOfBoundsException
    {
        System.out.println("Welcome to Parent Class");
    }
}
public class Child extends Parent
{
    public void display() throws Exception
    {
        System.out.println("Welcome to Child class");
    }
    public static void main(String args[]) throws Exception
    {
        //Create object for Child class
        Child c = new Child();
        c.display();
        
        //Create object for Parent class
        Parent p = new Parent();
        p.display();
     }
}

The Parent Class display() method throws ArrayIndexOutOfBoundsException exception and Child class overriden display() method throws Exception.  We all know that Exception class is the super class of all Exceptions, we cannot have the Child class method throwing a Super class Exception while Parent class method throwing a Sub class exception and hence we will be getting the below exception when we run the above code.

Output :

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Exception Exception is not compatible with throws clause in Parent.display()

	at com.javainterviewpoint.Child.display(Child.java:12)
	at com.javainterviewpoint.Child.main(Child.java:20)

4. When Parent class declares an Exception then the Child class overriden method  cannot declare super class exception

package com.javainterviewpoint;

class Parent 
{
    public void display() throws Exception
    {
        System.out.println("Welcome to Parent Class");
    }
}
public class Child extends Parent
{
    public void display() throws ArrayIndexOutOfBoundsException
    {
        System.out.println("Welcome to Child class");
    }
    public static void main(String args[]) throws Exception
    {
        //Create object for Child class
        Child c = new Child();
        c.display();
        
        //Create object for Parent class
        Parent p = new Parent();
        p.display();
     }
}

The Parent Class display() method throws Exception and Child class overriden the display() method and throws ArrayIndexOutOfBoundsException .  The Child class overriden method is allowed throw sub class exception, so the above code will run fine without any issue.

Output :

Welcome to Child class
Welcome to Parent Class

Leave a Reply

Your email address will not be published. Required fields are marked *