As shown in http://docs.oracle.com/javase/tutorial/java/IandI/override.html, Java does allow
Another to add here is: 1. Static methods belong at the class level. So u cannot override method in the derived class. as simple its called hiding. :) 2. Instance methods belong to the objects, so objects are overrided. So we can override in the derived class.
Above other comments give a good example have a look into it..
Regards Punith
Because, one are like Bananas and the other ones are Apples.
Explaination:
Example:
Foo.bar();
is something different than
new Foo().bar();
Guess which one is called?
Foo f = new Foo();
f.bar();
Simple answer: that would be the mess.
Concrete answer: what to call in that case Derived.foo()
? Base.foo()
can't be called as it's hidden (as per you), Derived.foo()
can't be called as it's not static.
I suspect it is to avoid confusion with dealing with the base class. In fact I imagine the designers didn't see an obvious way this should behave.
class Base {
static void foo () {}
}
class Derived extends Base {
void foo () {} // say this compiled
}
Base b = new Derived()
b.foo(); // should the static or the virtual method be called?
Should b.foo() call Base.foo() or should it potentially call Derived.foo()?