• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

storage of variable in super and sub classes

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {
int i=10;
}

class B extends A {
int i=20;

public static void main(String[] args) {
B b=new B();
}

can anyone please tell me how the "i" in A and how the "i" in B are represented in the object. are they stored seperately or not? if yes where??? else how the distinction is made between "b.i" and "super.i"???

please give me a good answer
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are "stored" in two separate classes. The relationship between the two clases is defined by B subclassing A, which allows B to access any protected methods or variables (there is a bit more to this point, actually, but I won't go into it unless asked) contained in A. Bottomline, A's i and B's i are two completely different variables. The distinction between b.i and super.i is implied, each refers to a different int. super.i is really just shorthand for 'an instance of my immediate superclass'.i. In this case, an object of class A. Also, in class B you could use this.i as shorthand for 'instance of this (class B) class'.i.

Hope this helps
[ January 28, 2005: Message edited by: Jason Fox ]
 
Sumesh Kumar T N
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much. I would also like to know the extra bit you talked about. Is the constructor getting called for the superclass or not ???
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A constructor is called for each class in the chain of superclasses, all the way up to Object.

If you implement a constructor, if the first statement is not a call to one of the superclass's constructors, the compiler inserts a call to the superclass's default (no argument) constructor as the first statement.

The overall effect of these rules is that Object's constructor body executes first, then the next descendent, then the next, down to the class being constructed. If B extends A and C extends B, the constructor body sequence will be Object, A, B, C.

The reason I say "constructor body sequence" is because, technically the call chain is the reverse of the above. C's constructor gets called first, but since the first staetment is a call to B's constructor, the effect is that the body of B's constructor completes before the rest of C's constructor executes. Did I just make that more confusing than it really is?

Long story short, when a class's constructor executes, it can count on the fact that its parent has initialized all of the parents fields.
[ January 30, 2005: Message edited by: David Harkness ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic