• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

polymorphism question

 
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following code sample...why would s1 be displayed and not s2?
class s1 {
String s = "s1";
void display(){System.out.println(s);}
}
class s2 extends s1{
String s = "s2";
public static void main(String args[]){
s2 x = new s2();
x.display();
}
}
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
when you calles x.display() execution will transfer to class s1 display() function and here value of s is "s1".
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Member variables don't follow the same rules as methods. If you had two methods, one in the parent and one in the sub, and then called display which ran one of the two methods, it would run the method of the subclass. But in your example, you are calling a variable, so it will call the variable of the parent class. Static methods work the same way as class variables.
Bill
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another example that might explain it better:

In a seperate file:

Output when executing Subclass:
Where am I now? Your inside: subclass
Where an I now? Your inside: subclass
Superclass
Notice that Super's getAnswer() says where am I? (w/o the now). This proves that the rules are different for methods and variables. The method of super was overridden so it didn't matter that I casted it to Super it still calls the subclass method. Subclass's variable answer however only hides the superclass's variable. So, when I cast it to Super...Super's answer is displayed. I hope that makes sense.
Joe
[This message has been edited by Joseph Russell (edited March 16, 2001).]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<code>
class s1 {
String s = "s1";
void display(){System.out.println(s);}
}
class s2 extends s1{
String s = "s2";
public static void main(String args[]){
s2 x = new s2();
x.display();
}
}
</code>
Because in the main method, you have called the inherited method display() defined in the superclass. Although you have hide the string s in the subclass, the inherited method display() has no way to know the string s is being hided. So it will access it's own member.
Correct me if I am wrong.
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have one more code for everyone to work outclass d {
final String b = "shabbir";
void display() {
System.out.println(b);
}
}
class e extends d {
String b = "shiva";

public static void main(String args[]) {

e i = new e();
i.display();
}
}

why this code is not giving error beacause we are trying to override member variable b which is declared final.
 
"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 NOT overriding the final variable. You can't cuz it's final. You are declaring a DIFFERENT variable with the same name in the sub-class which HIDES the final variable in the super class.
 
reply
    Bookmark Topic Watch Topic
  • New Topic