aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes polymorphism question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "polymorphism question" Watch "polymorphism question" New topic
Author

polymorphism question

Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
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

Joined: Jan 20, 2001
Posts: 115
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

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Jan 08, 2001
Posts: 290
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

Joined: Jan 15, 2001
Posts: 52
<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

Joined: Nov 12, 2000
Posts: 183
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

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: polymorphism question