Why doesn’t Java allow static methods to be hidden through instance methods?

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

>Override instance method by instance method > Hide static method by static method

My question is why Java does not allow static superclass methods to be hidden through instance methods This can be done by:

class Base {
    static void foo () {}
}

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

I don't see any special problems with the above method - it's just as (allowed) hidden static is already "chaotic / complex"

Solution

I suspect it is to avoid confusion with dealing with base classes In fact, I imagine designers don't see obvious ways that should work

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?

b. Foo() calls base Foo () should still call derived foo()?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>