This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
This is a question from Jaworski Quiz :
What is the output of the following program ?
----------------------------------------------------------------
class Question{
String s ="Outer";
public static void main(String[] args){
S2 s2 = new S2();
s2.display();
}
}
class S1{
String s ="S1";
void display(){
System.out.println(s);
}
}
class S2 extends S1 {
String s ="S2";
}
A.S1
B.S2
C.null
D.S1S2

I thought the program would output S2, by using the inherited dispaly method from S1 to display its won s variable, which I thought hides the s variable in S1.Apparently the correct answer is, A, the display method displays the s variable of the S1 class. I dont understand. Anyone can educate me.
Thanks,
Herbert.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Herbert,
Observe that S2 does not override the display method in S1. This makes the call to S2Object.display() and variable reference 's' to get resolved at compile time.
One related point to note here ( for the exam ) is that variables are not polymorphic. They are always bound at compile time.
Hope this helps.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Just to add to what Ajith has explained - All the variables are statistically bound and evaluated at the compile time where as all the methods barring private and static are dynamically bound and they are resolved at the run time.
I have modified code and added highlighted lines in the code given above.
class Question{
String s ="Outer";
public static void main(String[] args){
S2 s2 = new S2();
s2.display();
}
}
class S1{
String s ="S1";
void display(){
System.out.println(s);
}
}
class S2 extends S1 {
String s ="S2";
void display(){
System.out.println(s);
}

}
Now the output will be S2.
Hope this helps !!
Regards,
Milind

[This message has been edited by Milind (edited May 30, 2000).]
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Thanks Ajith and Milind.
Herbert.
Greg Whelan
Ranch Hand

Joined: May 18, 2000
Posts: 52
While playing around with Herbert's code, I hit a mutation that I thought exhibited some very odd behavior. I'll include my code here for the curious (hint: there's one key concept that the behavior hinges upon)...
Edward Man
Ranch Hand

Joined: May 16, 2000
Posts: 40
Let me expand the clue a little bit.
Any method that calls S1.display() method is statically binded to it because it is a private method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance
 
Similar Threads
It is so confusion Question about polymorphism.
method dynamic loading
HOW VARIABLES ARE DECIDED
Inheritence
Why doesn't the subclass variable hide it?