• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Inheritance

 
Ranch Hand
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 289
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ajith and Milind.
Herbert.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)...
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic