The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2001

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:

Be happy!!! Don't be sorry for someting which Java does.

Posted by Kishori Sharan on January 24, 2001 at 9:33 AM

Hi
Only non-static method calls are dynamically bound. Static binding applies to all fields refernces. When you create A a = new B() , then new B() creates an object of class A first ( with some restrictions) and then it creates object of class B, because an object of class B will have all non-private fields declared in class A, which is its ancestor. So at run time an object of class B will maintain two i variables one will be A.i which it inherits from class A and one will be B.i ( or simply i) which it has declared in its own class. Since reference a in A a = new B() ; is refering to an instance which has two i's and it requires A.i so it uses A.i. That is why redeclaring an instance variable in a sub-class is called name hiding. That is, A.i is also present because of inheritance, however A.i is just hidden because of B.i. Overriding never applies to fields. It only applies to non-static methods.

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