A static method cannot override an inherited instance method, but it can hide a static method if the exact requirements for overriding instance methods are fulfilled. A hidden superclass static method is not inherited. The compiler will flag an error if the signatures are the same but the other requirements regarding return type, throws clause, and accessibility are not met. If the signatures are different, the method name is overloaded, not hidden.
SCJP 5
Originally posted by Serch Hdez:
Hi there I'm having the same doubt I have already read the threads posted here, but now one explains why the compiler still complains, I have tested another situation pls look at the following code:
and...
the compiler complains that the implemented method m4 is not being overridden properly it says "cannot reduce visibility of inherited method from AbstractTest
I'm clear about the concepts but no one is explaining why there are compilation errors like this... and nobody posted examples of static methods where overriding rules could be applied.. can somebody explain why the compiler thinks you are trying to override the static method?..
I'm using eclipse to compile and build, thanks.
If you can't be the Sun ..<br /> Be a star ..............
SCJP 6.0 [95%] OCE EJBD 6.0 [93%]
class Cat extends Animal{
public static void main(String[] args) {
new Cat().overrideMe();
}
}
class Animal{
public static void overrideMe(){
System.out.println("in super class static method");
}
}
Originally posted by Campbell Ritchie:
No, static methods aren't inherited.
Why all the rules of overriding are followed when we try to override(i mean redefine) static methods in sub-classes?
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jitendra
SCJP1.5
SCWCD1.5
Thank you. I think that answers Ram Manoj's question too.Originally posted by Jitendra Jha:
Static methods are NOT inherited . . .
Originally posted by Jitendra Jha:
Static methods are NOT inherited in the true sense of inheritance...
They are inherited in the sense that they are in the scope of the subclass -- i.e., a static method Parent.method() can also be called as Child.method(), or just as method() withing the Child class.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Members of a class that are declared private are not inherited by subclasses of that class.
A class C inherits from its direct superclass and direct superinterfaces all non-private methods (whether abstract or not) of the superclass and superinterfaces that are public, protected or declared with default access in the same package as C and are neither overridden (�8.4.8.1) nor hidden (�8.4.8.2) by a declaration in the class.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Originally posted by marc weber:
My understanding is that static methods are inherited. However, because they cannot be overridden, they do not behave polymorphically.
Static methods are overridden for they follow rules of overriding for being inherited.
Static methods are not overridden in the sense of behaviour for they do not exhibit polymorphism.
Originally posted by Ernest Friedman-Hill:
The official statement from the language gods is that they are not inherited. But please note that this statement is only true for some specific definition of inheritance.
Jitendra
SCJP1.5
SCWCD1.5
------------------------
Bob
SCJP - 86% - June 11, 2009
SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Jitendra
SCJP1.5
SCWCD1.5
Jitendra
SCJP1.5
SCWCD1.5
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |