The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
June 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Overriding Static methods

Posted by KIshori Sharan on June 29, 2000 at 12:58 PM

There are two classes as folows:

class A {
static void display ( ){ System.out.println ( "Class A" ) ; }
}

class B extends A {
static void display ( ) { System.out.println ( "Class B" ); }
}

When we create an object and call the display ( ) method as follows.

A aa = new B() ;
aa.display ( ) ;

it prints "Class A". However, at runtime the aa object contains the reference of an object of class B. But if we call display ( ) method like
B bb = new B() ;
bb.display () ;
then it prints "Class B". However, if we remove the static keyword from method declaration of classes A and B then in both the cases it prints "Class B". I want to know what makes the method overriding different if the method is static or non-static. In case of static method overiding the method which is called is the method of the class the object is defined of irrespective of the class of the object of which it contains the reference i.e. even if we store derived class reference in ancestor class object it always calls the method of ancestor class.
Any clarification is greatly appreciated.

Thanx
Kishori





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us