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

Does method overloading implicitly typecasts to larger type even though compatible type is available

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I execute below program I expected to call child class method but it called parent class methods. Any idea  ?


 
Marshal
Posts: 74371
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't call classes the same as types supplied by the JRE, least of all if their names are the same as types in the java.lang package. That will store up trouble for you in months to come.
You aren't overriding any of those methods, but overloading them. The subclass inherits methods from the superclass, but they are the same methods as the superclass' methods. Since you declared the variable with the superclass' type, the subclass' overloaded methods aren't even visible to the compiler. In lines 6 and 8, you are getting promotion from int to double and calls to the methods from the declared type.
 
raghu kalachar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I know  why the subclass' overloaded methods aren't even visible to the compiler ? Even though methods with 'int' types are available why line 6 and 8 are getting promoted
 
Master Rancher
Posts: 4062
56
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you've told the compiler to treat d as a Parent, not a Child.

Yes, the actual object is really a Child.  But the reference d is interpreted as a Parent, and has only Parent methods available to it, as far as the compiler is concerned.
 
Mike Simmons
Master Rancher
Posts: 4062
56
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For comparison, try putting the following line into your main method:
 
raghu kalachar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for explanation.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic