This page topics close

If a child class defines a static method with the same signature as a static method of parent class , then it is known as Method Hiding.Now we can say that method in child class hides the method in Parent class.

Redefining a static method in child class with same signature is called method Hiding.

Redefining a instance method in child class with same signature(satisfying all overriding rules) is known as Method Overriding.

  • Example of Static Binding in java

    Create two classes Animal and Dog where Animal is a parent of Dog. define static methods in both classes with same signature.

    Example)
    drag_handle
     
    class Animal {
    	public static void eat() {
    		System.out.println("Animal is eating...");
    	}
    }
    
    class Dog extends Animal {
    	// hiding eat method of Animal class
    	public static void eat() {
    		System.out.println("Dog is eating...");
    	}
    }
    
    public class Example {
    	public static void main(String[] args) {
    		Animal a = new Animal();
    		Animal b = new Dog();
    		a.eat();// call eat method
    		b.eat();// call eat method
    	}
    }
    Output:
    Animal is eating...
    Animal is eating...

What next?

The next topic is instanceof operator in java

Share this page