• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method calling Doubt

 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic