Re: Static methods overridden !!



Ravi wrote:
Clearly saySomething comes from super class. This is confusing for
me.

The method-resolution mechanism moves up the inheritance hierarchy from the compile-time type to look for the first matching method at runtime. Since CowTest "is-an" AnimalTest, the invocation from CowTest can find the method in AnimalTest.

However, if CowTest did have a matching static method, it wouldn't override the superclass static method, it would hide the superclass static method. There is no polymorphism of static methods.

AnimalTest.saySomething() will never invoke CowTest.saySomething().

-- Lew
.



Relevant Pages

  • Re: Static methods overridden !!
    ... Since CowTest "is-an" AnimalTest, the invocation from CowTest can find the method in AnimalTest. ... However, if CowTest did have a matching static method, it wouldn't override the superclass static method, it would hide the superclass static method. ...
    (comp.lang.java.programmer)
  • Re: Static methods overridden !!
    ... public static void saySomething() { ... class CowTest extends AnimalTest { ... public static void saySomething1() { ... it compile as CowTest doesn't have the method saySomething. ...
    (comp.lang.java.programmer)