• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

confused with Overloading rules  RSS feed

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


From what I understand of the rules, if a method is an instance method what is executed is the type of the object and not of the reference. But here I'm confused that b1.print(new Base()); would print "super." I thought what happened here was that the print method from the SuperBase class was overloaded by the print method from the Base class. And given that the print(Base) method was the more specific of the two should have been executed.

But I was wrong as it printed "super." Further, I changed the reference for b1 to Base and run it again. This time it printed "base." So it seems like the reference is what matters to the determination of the method to be executed, which I'm not sure why it's the case since it's an instance method.

Anybody has an idea?

Thanks
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all Use Code Tags. That makes it much more readable.

b1.print(new Base()); B1 is a SuperBase thus it only has the method: void print(SuperBase a) and it is not overriden
in Base. If you would have used a Base reference instead of a SuperBase reference then the output would be different.
 
Ronwaldo Cruz
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. This answered my question
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!