Engineering Full Stack Apps with Java and JavaScript
I have seen many people arguing that static methods are not inherited. That is one misconception many beginning programmers have. According to true object oriented principles, static methods should not have inherited, but with Java a class does inherit all members of parent class including static.
So what is the answer to the question of if static methods are inherited? A simple answer as per Java Language Specification would be "Yes" for classes and "No" for interfaces:
The Java Language Specification 8, section 8.4.8, titled as 'Inheritance, Overriding, and Hiding' says that "A class C inherits from its direct superclass all concrete methods m (both static and instance)..." and "A class C inherits from its direct superclass and direct superinterfaces all abstract and default methods...".
The above section also say that this rule of inheritance for static methods does not apply for interfaces, as the specification also says "A class does not inherit static methods from its superinterfaces".
There are more constraints to these rules and for more details, please refer to the section mentioned above in the Java Language Specification 8.
So why people get fooled easily?
First, they refer to everything other than the official Java Language specification.
Also, there is a difference in the way these are treated when redefined in a subclass. Or to be more precise, only instance methods can be overriden. Instance variables, static variables and static methods are treated the same, and are not overriden in a subclass and the process is called as hiding.
You may try using the @Override annotation to see if an override is valid, over an instance method, static method, instance variable and static variable. You will be only allowed to place them over an instance method.
So what do you mean by overriding?
When you assign a child object to a parent reference, the actual member selected at run time for an overriden member will depend on the actual object used at runtime.
For non-overriden members like instance variables, static variables and static methods, the member selected will not depend on the actual object at runtime.
If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass. The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass. For a better clarity on overriding and hiding, please read the official Oracle notes that also give you some examples @ http://docs.oracle.com/javase/tutorial/java/IandI/override.html.
Yes, there is some confusion and hence many text books say it differently. All that matters is understanding how it works.
Comments
static members/methods in inheritance
class A {
static int i = 5;
static void m1();
}
class B extends A {
}
class TestStatic extends B {
public static void main() {
/*
in the above can i access the static members / methods?
if i access "A.m1()", then am i follow correct inheritance ? and
if i can directly call A.m1(); then what does the subclass inherits mean??
*/
}
}