Re: Static methods overridden !!



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.


My understanding of overriding is as follows. (Not polymorphism , just
sticking to overriding)

Class Super {
public void method1(){}
}
Class DerivedOne extends Super {
}
Class DerivedTwo extends Super {
public void method1(){}
}

public Class OverrideDemo {
public static void main(String a[]) {
new DerivedOne().method1(); //calls the Super class method1()
new DerivedTwo().method1(); //calls the DerivedTwo class method1()
}
}

I am not talking about polymorphism here, just overriding. If you
observe carefully, then the behavioiur of statis methods is exactly
similar. Assume that the method1 is static.
DerivedOne.method1() ; //will call Super static method method1.
DerivedTwo.method2(); //will call DerivedTwo static method method1.

Technically speaking static methods ARE OVERRIDDEN, not at the object
level ofcourse at the class level as invocation of statics is not
object dependent. Also, we cannot take the advantage of polymorphism
in case of the static methods but thats not the point. Static methods
are overridden.

Does my argument make sense?

Regards,
Ravi

.



Relevant Pages

  • Re: why cant a implment an abstract function as static?
    ... Java wont let you change context/from to static when overriding ... If a subclass re-declares a static method, it hides the base class method, ... public static void main{ ...
    (comp.lang.java.programmer)
  • Re: Generics and static members
    ... public void MethodB() ... If this is the case, then it's not possible for the compiler to fulfill your intent, because it needs to know when it compiles the _generic_ class how to get at the method, and at that point the type parameter hasn't actually been resolved yet. ... the compiler could generate some complex code to use reflection to determine at run-time the correct static method to call. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: My First C# (warning - long post)
    ... add(entry, entry); ... public void makeAnimalSound ... AnimalNoiseGenerator noiseGenerator = dictionary.get ... one of this objects vs calling a static method. ...
    (comp.lang.cobol)
  • Re: New C# programmer questions
    ... static void Main ... an object to leave a static method for a non-static method? ... public void MyOtherMethod() ... // do the last of the things, time to exit program...how to exit? ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Timer and Random
    ... OnTimerEvent(object source, EventArgs e), method is not a static method, its ... > that go into this Picture Box. ... > public void Presentation_Load ...
    (microsoft.public.dotnet.languages.csharp)