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.
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
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
posted
0
Since the methods work on RTTI, the derived class method is called. What do you mean RTTI?
"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 ]