This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes q on overloading and overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "q on overloading and overriding" Watch "q on overloading and overriding" New topic

q on overloading and overriding

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
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;

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

i answered b, but the correct answer is a.

When u say, Super s = (Super) t;
subclass is cast to be a superclass and the result is stored in superclass ref?
so, at runtime, s refers to Super?

SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Subclass Object is cast to superclass type. Still object is of subclass. Because of joava's runtime polymorphism, it will check the object to call correct version of method.

"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Consider Paul's rocket mass heater.
subject: q on overloading and overriding
Similar Threads
question from Sun Guoqiao's homepage
Question 58 jiris on casting
Polymorphism confusion
SCJP questions