This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method calling Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method calling Doubt " Watch "Method calling Doubt " New topic
Author

Method calling Doubt

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
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
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
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
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%
 
jQuery in Action, 2nd edition
 
subject: Method calling Doubt
 
Similar Threads
Majji exam1 Qno2
assignment
Question
Sub Class and super class
regarding subclassing