Re: Static methods overridden !!



Ravi wrote:
Well, the class doesn't have the method at all ! Please refer the
original code again. CowTest class doesn't have saySomething method at
all . It has saySomething1 (1 (one) extra in the end, may be I should
have made this more clearly differentiating). I am not referring the
objects here and I am clear on the fact that objects have nothing to
do with invocation of static methods.

The method-resolution mechanism moves up the inheritance hierarchy from the compile-time type to look for the first matching method. 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 !!
    ... 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. ...
    (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)