I understand that in Java
static methods are inherited just like instance methods, with the difference that when
they are redeclared, the parent implementations are hidden rather than overridden. Fine,
this makes sense. However, href="http://docs.oracle.com/javase/tutorial/java/IandI/override.html">the Java
tutorial notes that
Static methods in interfaces are never inherited.
Why? What's the
difference between regular and interface static
methods?
Let me clarify what I mean when I say
static methods can be
inherited:
class Animal
{
public static void identify() {
System.out.println("This is an
animal");
}
}
class Cat extends Animal
{}
public static void main(String[] args) {
Animal.identify();
Cat.identify(); // This compiles, even though
it is not redefined in
Cat.
}
However,
interface
Animal {
public static void identify() {
System.out.println("This
is an animal");
}
}
class Cat implements
Animal {}
public static void main(String[] args) {
Animal.identify();
Cat.identify(); // This does not compile, because
interface static methods do not inherit.
(Why?)
}
No comments:
Post a Comment