Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

q on overloading and overriding

 
Ranch Hand
Posts: 303
  • 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

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?
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
The City calls upon her steadfast protectors. Now for a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic