Method Overriding

When child class define some method which are already existing in parent class, this concept is known as method overriding.

When child class define same method then compiler assume that programmer is known to the fact that method from parent class is inherited in child class and if he is still defining same method it means programmer wants some other definition for the method.

Example:


  class one 
{
void show()
{
System.out.println("class one show method");
}
}
class two extends one
{
void show()
{
System.out.println("class two show method");
}
}
class th
{
public static void main(String arg[])
{
two t=new two();
t.show();
}
}
/*Output:
class two show method()
*/


Rules for Method Overriding

  • Method signature and return type must be same

    Method in both parent and child class must match return type, method name and parameters. if any of don't match then compiler will show an error.


    Example :

    
      class one
    {
    void show()
    {
    System.out.println("class one show method");
    }
    }
    class two extends one
    {
    int show() // Error: Method must be same
    {
    System.out.println("class two show method");
    }
    }
    Error two.java:10: error: show() in two cannot override show() in one
    int show() // Error: Method must be same
    ^
    return type int is not compatible with void
    1 error

  • Overriding method can't reduce access level of overridden method.

    Overriding method can't reduce access level of overridden method. It means if a method declared in parent class with public access specifier then child class can't override this method with protected or private access specifier.



    Example :

    
      class one
    {
    public void show()
    {
    System.out.println("class one show method");
    }
    }
    class two extends one
    {
    private void show() // Error: access level can't be decreased.
    {
    System.out.println("class two show method");
    }
    }

    Error two.java:10: error: show() in two cannot override show() in one
    private void show() // Error: access level can't be decresed.
    ^
    attempting to assign weaker access privileges; was public
    1 error


    Reverse of above is true means access level can be broaden. for example a private class overridden method can be assigned protected or public access privilege in child class.


  • Final method can't be overridden in java

    Child class can't override final method.


    Example :

    
      class one
    {
    final void show()
    {
    System.out.println("class one show method");
    }
    }
    class two extends one
    {
    void show() // Error: final method
    {
    System.out.println("class two show method");
    }
    }

    Error two.java:10: error: show() in two cannot override show() in one
    void show() // Error: final method
    ^
    overridden method is final
    1 error


  • Overriding method can't throw checked exception which is higher than exception thrown by overridden method

    Overriding method can thrown any unchecked exception but it can't throw checked exception which is higher in exception hierarchy than checked exception thrown in parent class.


    For example say show() method in parent class throwing a IOException which is checked exception. In child class show() method throwing Exception which is higher in exception hierarchy then IOException. compiler will generate following error.


    
      class one
    {
    void show() throws IOException
    {
    System.out.println("class one show method");
    }
    }
    class two extends one
    {
    void show() throws Exception // can't throw higher exception
    {
    System.out.println("class two show method");
    }
    }

    Error two.java:10: error: show() in two cannot override show() in one
    void show() throws Exception // can't throw higher exception
    ^
    overridden method does not throw Exception
    1 error