Forums Register Login

q on overloading and overriding

+Pie Number of slices to send: Send
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

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?
+Pie Number of slices to send: Send
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.
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 687 times.
Similar Threads
Inheritance.
Question 58 jiris on casting
Polymorphism confusion
question from Sun Guoqiao's homepage
SCJP questions
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:00:50.