• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

question from Sun Guoqiao's homepage

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the methods work on RTTI, the derived class method is called.
What do you mean RTTI?
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic