File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

polymorphism question

 
Ronnie Phelps
Ranch Hand
Posts: 329
  • 0
  • Mark post as helpful
  • send pies
  • 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();
}
}
 
Ishaan Mohan
Ranch Hand
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • 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".
 
bill bozeman
Ranch Hand
Posts: 1070
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Joseph Russell
Ranch Hand
Posts: 290
  • 0
  • Mark post as helpful
  • send pies
  • 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).]
 
Wong KK
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
shabbir zakir
Ranch Hand
Posts: 183
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic