Method Hiding In Java

Method hiding is related to method overriding.In java cannot override static methods because method overriding is belongs dynamic binding at runtime and static methods are bonded using static binding at compile time.
So if you are declare a method with same name and signature in sub class which does look like you are override static method in Java but in reality that is method hiding.
 

public class Animal {
    public static void eat() {
        System.out.println("Dog");
    }
}
public class Lion extends Animal {
    public static void eat() {  // hides Animal.eat()
        System.out.println("Lion");
    }
}

Here, Lion.eat() is said to hide Animal.eat(). Hiding does not work like overriding, because static methods are not override.

Related Articles

post a comment