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
the answer is 7
A simple procedure to remember is :
1) All methods are invoked according to the object type of the variable, but for that the same signature method has to exist in the reference type otherwise its a compiler error.
2) All variables (static, nonstatic) are invoked according to the reference type of the variable.
Here the reference type of p is Process and object type is InheretanceTest.
The program invokes with a char argument, and you see there is no char argument method in Process. But still compiler promotes char to int and thereby maintains its rule for method invoction.
if you add the follwoing method in Process, then the method invoked will be from InheretanceTest
char InheritanceTest(char c) {
c='V';
return (char)c;
}
I know its confusing but if you give it some time you'll understand. Hope it helps.
Asma Zafar.
Sun Certfied Programmer for Java2 Platform.
=====i can understand both the rules of 1 and 2.but i think this stmt is misleading-
**The program invokes with a char argument, and you see there is no char argument method in Process.**
FOr meth invocation it checks the ob type in this case inheritencetest...but this method is overloaded and thus b'coz of implicit conversion chooses the meth in base calss with int arg. correct me if iam wrong