Why doesn't Java allow hiding static methods by instance methods?

后端 未结 4 2061
旧时难觅i
旧时难觅i 2021-02-20 03:25

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. Hidin
4条回答
  •  再見小時候
    2021-02-20 04:27

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

提交回复
热议问题