Win a copy of Murach's MySQL this week in the JDBC and Relational Databases 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overloaded method

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why lines #1 and #2 are calling SuperCafe4Java get() method?
the actual object is SubCafe4Java and it has more specific get(String) method.

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this program get() method is overloaded so


SuperCafe4Java superFoo = new SubCafe4Java();

System.out.println (superFoo.get(new String("super")));//#!

#1 : superFoo.get() method is called based on the reference not on runtime object type.

Output of this program is

SuperCafe4Java
SubCafe4Java
SuperCafe4Java
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you change the object to String you will get the desired output.

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

waht you did ? , just overriding.

with overriding we can achieve runtime polymorphism while using overloading we can achieve compile time polymorphism.

it means in case of overloading method called using refeneces.


---please correct me if i am wrong.
reply
    Bookmark Topic Watch Topic
  • New Topic