Sri
----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
Sri
Sri
Originally posted by Mindy Hudson:
class InheritanceTest extends Process {
int x=18;
public static void main(String [] args) {
Process p = new InheritanceTest();
System.out.println(p.InheritanceTest('R'));
System.out.println(p.x);
}
InheritanceTest() {
System.out.println(true ^ true);
}
InheritanceTest(char c) {
System.out.println(c);
}
char InheritanceTest(char c) {
c='V';
return (char)c;
}
}
class Process {
int x=9;
Process() {
System.out.println("Starting Process...");
}
char InheritanceTest(int i) {
i='S';
return (char)i;
}
}
What is the Output?
1.Prints Starting Process �, false, �S� and 18
2.Prints false, �V� and 9
3.Prints true, �V� and 9
4.Prints Starting Process � , true, �V� and 9
5.Prints Starting Process �, false, �V� and 9
6.Prints Starting Process �, false, �V� and 18
7.Prints Starting Process �, false, �S� and 9
8.Prints Starting Process �, true, �R�, and 18
9.Prints Starting Process �, true, �V� and 18
I think the answer should be 5.But the correct answer is 7.Since p has its class as InheritanceTest shouldnt the method in InheritanceTest be called.Can you explain it to me.
Sri
----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
Originally posted by Gaurav Mantro:
OK, I was partially wrong in my answer earlier. I overlooked some of the things, my bad.
Bala gave the right answer.
Since InhertitanceTest() in Process has int argument and InheritanceTest() in InheritanceTest has char argument, there would be no overriding. Since char can be converted to int the InheritanceTest method of Process is invoked.
So as long as InheritanceTest() in the process has argument to which char can be converted implicitly and that method is not overridden in InheritanceTest class, InheritanceTest of Process class will be invoked.
Sri did that help ?
Sri
Originally posted by seema ram:
I dint completely understand the explanation.As the class is InheritanceTest I thought it will first look into InheritanceTest for the corresponding method.And since InheritanceTest(char c) was there would execute it.So the question of overriding never comes in.Can somebody correct me.
Sri
- Siva<br /> Sun Certified Programmer for the Java™2 Platform
Consider Paul's rocket mass heater. |