• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance & overriding problem

 
Sigrid Kajdan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

following is a slightly extended example taken from the Java Language specification (end of paragraph 12.5 in Execution chapter). I added the variable int three = 99 to the superclass, suspecting that now, the superclass constructor would - as before - call the overriding subclass method but use its own instance variable, so print 99 3 instead of 0 3.
This was not the case, so I tried using the superclass as the reference type - I used to think that if you have a subclass instance but superclass reference, you get the SUBCLASS METHOD (virtual invocation) but SUPERCLASS FIELD. But, here too, I get the output 0 3 all the same. Can anybody explain this to me?




Thank you, greetings,
Sigrid
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sigrid Kajdan:
...I used to think that if you have a subclass instance but superclass reference, you get the SUBCLASS METHOD (virtual invocation) but SUPERCLASS FIELD...

Not unless you preface the field name with "super."

See JLS 8.3.3.2 Example: Hiding of Instance Variables.
 
Sigrid Kajdan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But then, how about this example from Joshua Engel's "Programming for the Java virtual machine"?

 
Roger W. Chang
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From K&B book pg.98
Polymorphic method invocations apply only to instance methods...Not static methods. Not variables...


So in your first example


"not" refers to a NotYetInitializedTST object so invoking printThree() will call NotYetInitializedTST's printThree().

if you had

you would get an output of 99.

Hope that helps,
Roger
 
S Vilish
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a sub class object is created, following sequence is executed.

1. Super class static block
2. Sub class static block.
3. Instance variable Initialization of super class
4. constructor of super class
5. Instance variable Initialization of sub class
6. constructor of sub class.

In case of overriding, method will be called as per the invoking object.

0 is coming becoz three hasn't been initialised in the sub class.

Hope this clears your doubts.

Vilish
 
Sigrid Kajdan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again, thank you all for your help!

I fear I'm still not totally clear about when to use the subclass' and when to use the superclass' instance variable.
What I know so far is, in general you get method of subclass, but field of subclass only if subclass instance variable is used as a REFERENCE. But there seems to be a special case when you don't have a simple method outputting static text like the one from my "Programming for the JVM" example above, but a method which uses the instance vars.
Are there any general rules which class' vars it uses?

For instance, I experimented with the code from Java Language Specification paragraph 8.3.3.2 "Hiding of instance variables". As it is, the code uses a method "printBoth" which is present only in the subclass. Now, I added a method "printMine" to the superclass, which is inherited by the subclass:

class Point {
int x = 2;
public void printMine() {
System.out.println(x);
}
}

public class HidingTST extends Point {

double x = 4.7;
void printBoth() {
System.out.println(x + " " + super.x);
}
public static void main(String[] args) {
HidingTST hid = new HidingTST();
hid.printBoth();
System.out.println(hid.x + " " + ((Point) hid).x);

// now use a method which is in both classes!
hid.printMine();
//now use superclass reference for this method!
Point hid2 = new HidingTST();
hid2.printMine();

}

}

I thought that with this method, I would get different results whether I had a reference to the subclass or to the superclass. But I get the superclass field in both cases! So is it really the case that if I don't override a method, and this method uses fields, it always uses the superclass' fields?
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sigrid,

the point here is that you have the printMine() method only in the superclass. So when calling it, no matter from a subclass or supercalss reference, always this very method is called.
Since this method is in the superclass, it will use its own x-variable, as it doesn't know about the subclass object's x-variable (because the superclass doesn't care which subclasses it has, but the other way around, the subclass well knows its superclasses!)
This is why two times "2" is printed. Debug it and inspect the variables. Also, add the same printMine() method in the subclass and watch what is happening now.

Good luck.
 
Sigrid Kajdan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tommaso,

please excuse my answering you so late... but you really helped me, I think I got it now!
Also, your motto about understanding binary, which I like very much (although I really am an absolute beginner!) inspired me to use javap and then it was clear - the method (be it subclass or superclass) of course just uses its local variable "this" (aload_0) to get the field...

So thanks again, bye,
Sigrid
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic