This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question from Sun Guoqiao's homepage Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "question from Sun Guoqiao Watch "question from Sun Guoqiao New topic
Author

question from Sun Guoqiao's homepage

Ray Chang
Greenhorn

Joined: Sep 10, 2002
Posts: 25
What is the output of trying to compile and run the following code?
(Select one correct answer)
-----------------------------------------------------------------------
public class Test058 extends Super
{
public static void main(String args[]) {
Test058 t = new Test058();
Super s = (Super)t;
s.method('a');
}
public void method(int i){System.out.println("base int");}
public void method(char c){System.out.println("base char");}
}
class Super
{
public void method(int i){System.out.println("super int");}
public void method(char c){System.out.println("super char");}
}
-----------------------------------------------------------------------
A: base char
B: super char
C: base int
D: super int
answer is A. Why? Thanks
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
The answer is correct. I think your confusion is becuse of the output "base char" from the derived class - which is the way the program is coded. Change the println() statements to print 'Test058 char' instead. Since the methods work on RTTI, the derived class method is called.
Ray Chang
Greenhorn

Joined: Sep 10, 2002
Posts: 25
Since the methods work on RTTI, the derived class method is called.
What do you mean RTTI?
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

"up down"-
Welcome to the JavaRanch! Please adjust your displayed name to match the JavaRanch Naming Policy.
You can change it here.
Thanks! and again welcome to the JavaRanch!
________________
RTTI - Run Time Type Identification
Method Invokation -- uses the class of the current object denoted by the reference that determines which method is invoked.
(in your case the class of the current object is of type Test058, so it prints "base char")
Variable access -- uses the type of the reference
[ September 10, 2002: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: question from Sun Guoqiao's homepage
 
Similar Threads
SCJP questions
Doubt in Mock Question
q on overloading and overriding
Why its calling super class method?
Question 58 jiris on casting