• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

HOW VARIABLES ARE DECIDED

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Q{
public static void main(String args[]){
S2 s2ref = new S2();
S2.display();
}
}

class S1{
String s="s1";
void display(){
System.out.println(s);
}
}
class S2 extends S1{
String s= "S2";
}
On running the program, display method will display variable of Class S1;
I fail to understand why do display() method display variable S1, AND why not S2.
Thanks for help.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because when a variable of an object is accessed using a reference, it is the type of reference, not the class of the current object that determines which variable be actually accessed. thats why, the variable in s1 gets called. i hope, it will clear your doubt.
 
ZEESHAN AZIZ
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank for your reply, I am still not clear.At 1 , class of reference of s2ref is S2, not S1 . Right.
So that is why I think that I should have got display of s2, not s1.
public class Q{
public static void main(String args[]){
S2 s2ref = new S2();//at 1
s2ref.display();
}
}

class S1{
String s="s1";
void display(){
System.out.println(s);
}
}
class S2 extends S1{
String s= "S2";
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this code:
class S1{
String s="s1";
void display(){
System.out.println(s);
}
}
The address of the variable used in the display method is determined at compile time - NOT at runtime. ONLY methods are looked up dynamically, not variables.
The code:
class S2 extends S1{
String s= "S2";
}
Declares a new variable s that "shadows" the S1.s variable. If you had written:
class S2 extends S1{ s= "S2"; }
Then the output would indeed be S2
Bill

------------------
author of:
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I tried the second method suggested by Bill, but I get error.
"Identifier expected". Why do we need to declare the variable s again?
Vanitha.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point is that the S2 subclass is TRYING to inherit the s variable from S1. If S2 has NO s declarations (notice that I did not say references, I said declarations) then it can print out the s variable from the parent just fine. The subclass can even reset the value of the s variable that it inherited to be the value "S2" if it wants.
The problem comes when you have ANOTHER declaration of the s variable in the subclass. Now the compiler has to decide which s you are talking about. The one that you WOULD HAVE inherited or the one that you declared. Well, it will always decide that if you declared an s in the subclass, then that must be the one that you are talking about. You have effectively "hidden" the one that you could have inherited.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cindy Glass:
The subclass can even reset the value of the s variable that it inherited to be the value "S2" if it wants.


Hi Cindy,
Thanks for your reply. I tried to reset the value of s that's when i got that error. Am I missing something?
class S2 extends S1{ s= "S2"; }
Vanitha.
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The String s is an object field in the superclass. So I just can't say s = "S2"; (how did I miss this point?)
I have changed the code little bit.
This will print S2.

correct me, if I am wrong.
Vanitha.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
run this code and you will get difference
public class Q{
public static void main(String arg[]){
S2 obj=new S2();
System.out.println(obj.s);// line-1
obj.disply();// line-2
}
}
class S1{
String s="S1";
void disply(){
System.out.println(s);
}
}
class S2 extends S1{
String s="S2";
}
Amit
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, Cindy, when running the original code posted by ZEESHAN AZIZ, the S2.s variable should have taken precedence over the S1.s variable, right? If I read your post right, that's what you were saying.
But our debate is why does the display() method (when called from an instance of S2) choose the S1.s variable instead of the S2.s variable?
I'm still confused as to why this happens. The only thing I can think of is that display() is not overridden in S2, so display() is taking the this.s variable and this is referring to S1, not S2? But doesn't that contradict polymorphism concepts of being able to use the inherited methods from the superclass to apply to the instance of the subclass? Do you have to use the inherited variables as well, without shadowing them?
What if you wanted s to be an int in S2 and you wanted display() to print the int 2 instead of a string? (I know that ints convert to Strings for the purpose of the System.out.print() method, but with polymorphism, we shouldn't have to care.) Wouldn't it make sense to shadow that variable in S2 as:
int s = 2;
?
Confusing at best.
Thanks for answers.
April
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are probably confused because I responded to that query as though there was a display() method overriding the one in the super class, so I probably caused your confusion - sorry. Comes from looking at my code AFTER I have been playing with it instead of looking at the code in the question .
But what you said is basically correct. Polymorphism decides which method is going to get executed. Once the decision is made that the super classes' method is going to be IT, then the polymorphism is over. The superclass does not then look DOWN to see if any sub-class as a variable with the same name. As Bill said, the variable that is to be used was decided at compile time. You are sitting in the super class executing a method of the superclass, it is going to use the variables of the superclass.
If you WANT the variable of the subclass to be used, you need to override the method in the subclass so that at compile time the variable of the subclass is used.
 
April.Johnson
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Cindy, that clears this issue up and makes polymorphism in general more clear to me.
Appreciate it!
April
 
ZEESHAN AZIZ
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for your valuable response. It indeed helped me in clarifying my concept on the topic.
Thanks once again.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic