11

As shown in http://docs.oracle.com/javase/tutorial/java/IandI/override.html, Java does allow

  1. Overriding an instance method by an instance method and
  2. Hiding a static method by a static method

My question is why Java doesn't allow hiding a static superclass method by an instance method. This could be done like this:

class Base {
    static void foo () {}
}

class Derived extends Base {
    void foo () {}
    void access () {
        foo ();
        Base.foo ();
    }
}

I don't see any particular issue with the above approach - it is only as "messy/complex" as the (allowed) hiding of statics already is.

mafu
  • 31,798
  • 42
  • 154
  • 247

4 Answers4

12

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()?

mafu
  • 31,798
  • 42
  • 154
  • 247
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • basically I think it boils down to this http://stackoverflow.com/questions/610458/why-isnt-calling-a-static-method-by-way-of-an-instance-an-error-for-the-java-co – Liviu T. Jan 30 '13 at 11:35
5

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.

Archer
  • 5,073
  • 8
  • 50
  • 96
  • 1
    Good example. I think this should result in a compile time error for the reasons you gave. Instead, the code should either `Base.foo()` or `new Derived().foo()` explicitly. – mafu Jan 30 '13 at 11:07
1

Because, one are like Bananas and the other ones are Apples.

Explaination:

  • Static Methods are created when reading the Class-Structure
  • Methods are created when a object of a class is created.

Example:

Foo.bar();

is something different than

new Foo().bar();

Guess which one is called?

Foo f = new Foo();
f.bar();
Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79
  • From my point of view (which is biased by C#) it is clear: The method was called on an object, so the instance method is called instead of the static method. In Java though, I think it is allowed to call static methods on an object (design mistake imo), which leads to the confusion you're explaining. – mafu Jan 30 '13 at 11:04
1

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

Punith Raj
  • 2,164
  • 3
  • 27
  • 45
  • The question is why we can't hide static methods by instance methods (opposed to hiding with static methods, which works fine as you explained). – mafu Jan 30 '13 at 11:27