/java/

Different between hiding a static method and overwriting an instance method.

2015-04-15 20:30:55

package pl.btbw;

public class Animal {

    public static void testClassMethod() {
        System.out.println("Animal static ClassMethod");
    }

    public void testInstanceMethod() {
        System.out.println("Animal InstanceMethod");
    }

}
package pl.btbw;

public class Dog extends Animal {

    public static void testClassMethod() {
        System.out.println("Dog static ClassMethod");
    }

    public void testInstanceMethod() {
        System.out.println("Dog InstanceMethod");
    }

}
package pl.btbw;

public class App {

    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.testClassMethod();
        dog.testInstanceMethod();

        Animal animal = dog;
        animal.testClassMethod();
        animal.testInstanceMethod();

    }

}

Result

Dog static ClassMethod
Dog InstanceMethod

Animal static ClassMethod
Dog InstanceMethod