aspose file tools*
The moose likes Beginning Java and the fly likes storage of variable in super and sub classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "storage of variable in super and sub classes " Watch "storage of variable in super and sub classes " New topic
Author

storage of variable in super and sub classes

Sumesh Kumar T N
Greenhorn

Joined: Jan 26, 2005
Posts: 10
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
Jason Fox
Ranch Hand

Joined: Jan 22, 2004
Posts: 114
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

Joined: Jan 26, 2005
Posts: 10
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 ???
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: storage of variable in super and sub classes