GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Basic Java Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic Java Question" Watch "Basic Java Question" New topic
Author

Basic Java Question

chandra sundaresan
Greenhorn

Joined: Jan 14, 2004
Posts: 1
class Super
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class Runner
{ public static void main( String argv[] )
{ Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();
}
}
I was expecting the output as 5,Super instead of 5,Sub. I need to know how the above code works?
Tom Cockerline
Greenhorn

Joined: Dec 20, 2003
Posts: 7
Hi Chandra,
This is an example of the JVM determining which method to use based on the type of the object on which the method is called, rather than based on the type of the object's reference variable.
In Java, a reference variable can refer to an object of its own type or to any subclass of its own type. This is known as polymorphism. The sup reference used in the call to the printVal method,

was declared as type Super, but it refers to a Sub object:

Under these circumstances, the JVM will run the method belonging to the class of the actual object (Sub) rather than the method belonging to the class of reference variable (Super).
This important topic is dealt with at greater length and with much greater clarity by Sierra & Bates in Chapter 7 of their book "Head Start Java". I heartily recommend it for your further edification.
Tom Cockerline
Anand Sidharth
Ranch Hand

Joined: Dec 17, 2003
Posts: 44
This is the concept:
"Variables can also be overridden, it�s known as shadowing or hiding. But, member variable references are resolved at compile-time. So at the runtime, if the class of the object referred by a parent class reference variable, is in fact a sub-class having a shadowing member variable, only the parent class variable is accessed, since it�s already resolved at compile time based on the reference variable type. Only methods are resolved at run-time."
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Hi guys,

In the code I copied at line //1, does this mean that because the index is a member variable of super class this would always be shown from the super class? //created the object, this is a Super reference of a Sub object??
if this is so, would this mean that the reference calls the member variables???
And in line //2, because the object is of sub this means that the compiler takes the subs version of the method printVal()?
I think what I still can't get my head around is that the super member variable gets called.
I have read how my dog learned polymorphism, but i need it explained to me as if i were in primary school
Davy


How simple does it have to be???
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Thanks Yall, it just so happens that I was struggling with a program that called an extension class, and I couldn't remember how to instantiate it.
lol, saved me a long post.
Thanks again
Steve
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
I think what I still can't get my head around is that the super member variable gets called.

Don't confuse this with the super command in java. This is just a class and a constructor in the program called Super (notice the capitilized 'S').
the super() command will call from the root constructor. Meaning if a class is setup to extend a class, it is just that, an extension of that class. And when the command super("some object") is called it will insert that object into the root class constructor.
the command:
Super sup = new Sub(); creates a new Sub object....Read the posts above on this one.
I don't know if you were confused with the super command on your question.
HTH
M2k
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
No not confused,
but I can see where you were going. you could look at what I was asking as:

I was asking if member variable are called by the reference TopClass and the methods called by the object BottomClass?!
Davy
 
GeeCON Prague 2014
 
subject: Basic Java Question