• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic Java Question

 
chandra sundaresan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic