This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
<SCJP> says "...Remember that static methods can't be overridden! This doesn't mean they can't be redefined in a subclass, but redefining and overriding aren't the same thing."
But this code confused me !
When I compile the code , the compiler said that "info() in Apple cannot override info() in Fruit; attempting to assign weaker access privileges"
It means static method can be overridden ? But <SCJP> says they cannot be overriden !
class Fruit {
public static void info() {
System.out.println("fruit");
}
}
class Apple extends Fruit {
}
Here class level method info is extended in Apple, now if you will call Apple.info() this will five output as Fruit.
This means you wont achieve the intended purpose of overriding, i.e. calling a method depending upon its object at run time, so overriding has no effect but you have that method extended and access to code will be resolved at compile time you will get error if you happen to violate the overriding rules.
just because you got compiler error that don't assign weaker privileges does not prove that static methods can be overridden. for a moment ignore the error. or better assing public instead of protected. then create a new class and in the main method do this
it will print fruit. had there been overriding it would have print apple. overriding happens at runtime. it is late-binding. which method to called is decided at runtime. but here the call is decided on the basis of TYPE of f, which is Fruit. so no overriding.