Sample 1:
class Animal {
public static void saySomething() { System.out.print(" Gurrr!");
}
}
class Cow extends Animal {
public static void saySomething() {
System.out.print(" Moo!");
}
public static void main(String [] args) {
Animal [] animals = {new Animal(), new Cow()};
for( Animal a : animals) {
a.saySomething();
}
new Cow().saySomething();
}
}
Output is:
Gurrr! Gurrr! Moo!
Sample 2:
class Animal {
public void saySomething() { System.out.print(" Gurrr!");
}
}
class Cow extends Animal {
public void saySomething() {
System.out.print(" Moo!");
}
public static void main(String [] args) {
Animal [] animals = {new Animal(), new Cow()};
for( Animal a : animals) {
a.saySomething();
}
new Cow().saySomething();
}
}
Output:
Gurrr! Moo! Moo!
I just don’t understand why making saySomething non-static causes the second call to saySomething invoke the Cow version instead of the Animal version. My understanding was that Gurrr! Moo! Moo! would be the output in either case.
static methods are bound to their class at compile time and cannot be used polymorphically. When you declare a “static” method on Animal, it is forever bound to the Animal class and cannot be overridden. Static methods are bound to the Class object, not an instance of the Class.
Regular methods are bound at runtime and so the JVM can look at your call to “saySomething” and attempt to determine if you’re passing around a subclass of Animal and if so, has it overridden the
saySomething()method. Regular methods are bound to an instance of an object, not to the Class itself.This is also why you could never do this:
Since “static” means “bound at compile time”, it never makes sense for something to be static and abstract.