Win a copy of Head First Android this week in the Android 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Overloading

 
Ranch Hand
Posts: 69
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Q:What is the result of attempting to compile and run the program?
A: prints: m3Cm2Bm1A

My doubt here is in the line x, we have a method called m2(x); Here x is a subclass reference ie,new C(). But there is no overloaded version of m2 accepting a instance of class C. How can superclass references substitute for Subclass reference??

(added [C0DE][/C0DE tags, and removed Graemlin)
[ August 31, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An object of type C isa B isan A, by virtue of the class hierarchy. So the object can be passed to a method taking a reference to an object of types C, B, or A. Now in the case of method m2 there are two possibilities only: m2(A x) or m2(B x). The compiler chooses the method with the most specific type (the closest one to C in the hierarchy), and that is type B. So m2(B x) gets called.

By the way I believe this is one of Dan Chisholm's questions so please mention that when posting questions from his mock exams.
 
reply
    Bookmark Topic Watch Topic
  • New Topic