Method override in java

Method Overriding In Java :

  • When a method in subclass same name and same type signature as method in super class then this method is called overridden method and this process is method overriding.
  • Without inheritance method overriding is not possible.
  • Method overriding used for runtime polymorphism.
Method Override In Java

Rules of method overriding

  1. Method must have same name as the parent class.
  2. Method must have same parameter as in the parent class.
  3. Must be IS – A relationship.
  4. Argument list should be exactly the same as that of the overridden method.
  5. Return type should be the same or a subtype of the return type declared in the original overridden method in the super class.
  6. The access level cannot be more restrictive than the overridden method’s access level.

For example: If the super class method is declared public then the over-ridding 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. For example, private, protected, final, static method.
  • 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.
  • Constructor cannot be overridden.
  • Methods may be overridden: public.
  • We should always override abstract methods of the superclass.
  • Polymorphism doesn’t apply to static methods.

For example:

  1. Company contain main head office, other branches. That is contain the address. 
  2. Here two class first is human and second class is boy. Boy class extends human class. Both class have a common method eating.
class Human {
    public void eat() {
        System.out.println("Human is eating");
    }
}

class Boy extends Human {
    @Override
    public void eat() {
        System.out.println("Boy is eating");
    }

    public static void main(String args[]) {
        Boy obj = new Boy();
        obj.eat();
    }
}

Output :

Boy is eating

Super using overriding

  • super keyword is used for calling the parent class method/constructor.

We override a method in child class, then call to the method using child class object calls the overridden method. By using super we can call the overridden method as shown in the example below:

class Human {
    public void eat() {
        System.out.println("Human is eating");
    }
}

class Boy extends Human {
    @Override
    public void eat() {
        super.eat();
        System.out.println("Boy is eating");
    }

    public static void main(String args[]) {
        Boy obj = new Boy();
        obj.eat();
    }
}

Output

Human is eating
Boy is eating

Multilevel Method Overriding

//Base Class
class Parent {
    void show() {
        System.out.println("Parent display");
    }
}

//Inherited class
class Child extends Parent {
    //This method overrides show() of Parent
    @Override
    void show() {
        super.show();
        System.out.println("Child display");
    }
}

//Inherited class
class GrandChild extends Child {
    //This method overrides show() of Parent
    @Override
    void show() {
        super.show();
        System.out.println("GrandChild display");
    }
}

public class Main {
    public static void main(String args[]) {
        Parent obj1 = new GrandChild();
        obj1.show();
    }
}

Output

Parent display
Child display
GrandChild display

Abstract method and Overriding

abstract class Parent {
    public abstract void display(int x, String j);

    void nonAbstractMethod(){
        System.out.println("Non - abstract method");
    }
}

//Subclass or Derived Class
class Child extends Parent {

    @Override
    public void display(int x, String j) {
        super.nonAbstractMethod();
        System.out.println("child method is executed");
    }
}

public class Main {
    public static void main(String args[]) {
        Parent childObject = new Child();
        childObject.display(1, "Alive is Awesome");
    }
}

Output

Non – abstract method
child method is executed

Static method cannot override

public class Tenor extends Singer {
    public static String sing() {
        return "Tenor";
    }

    public static void main(String[] args) {
        Tenor t = new Tenor();
        Singer s = new Tenor();
        System.out.println(t.sing() + " " + s.sing());
    }
}

class Singer {
    public static String sing() {
        return "Sing";
    }
}

Output

Tenor Sing

Can we access the method of the superclass after overriding ?

  • Yes, to access the method of the superclass from the subclass, we use the super keyword.

Overriding Java Constructor

  • Java constructor cannot be the same for a parent class and a subclass because it must always be the same as the class name.