• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Overloaded method

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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.
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic