What will happen if you compile/run the following code?
A) prints "Show method in Test class" "Show method in Q2 class" "Show method in Q2 class" "Show method in Q2 class"
B) prints "Show method in Test class" "Show method in Q2 class" "Show method in Test class" "Show method in Test class"
C) prints "Show method in Test class" "Show method in Q2 class" "Show method in Test class" "Show method in Q2 class"
D) Compilation error.
The Answer is D ... please explian me WHY [ September 14, 2005: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
posted
0
1. Please use tags around your code, there are even buttons at the bottom of the editing window for that purpose.
2. Please, as a courtesy to the author and to let people know where to find the original mock exam, quote the source of the problem.
3. Have you compiled and/or run the code yourself? What did it print? Which of the options did you think was correct? What is the compilation error?
(That's the moderating task, over - no time at the moment to consider the question, sorry, got to go to work.) [ September 14, 2005: Message edited by: Barry Gaunt ]
The out put I got is ---------- javac ---------- Q2.java:22: incompatible types found : Test required: Q2 q = t; ^ 1 error
PLease it is showing compliation error please explain it
anand phulwani
Ranch Hand
Joined: Sep 10, 2005
Posts: 242
posted
0
i hope u know Casting, the error is because you need casting in the statement q = (Q2)t; because we are assigning the subclass object with its superclass object after that the program will run
but i think i should tell you that the output is going to be
Show method in Test class Show method in Q2 class Show method in Test class Show method in Q2 class
because you created Test t = new Test(); Q2 q = new Q2();
and if there static methods exist the static methods will be called irrespective of which class object they contain
Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%