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

后端 未结 4 2134
旧时难觅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:16

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

提交回复
热议问题