• Post Reply Bookmark Topic Watch Topic
  • New Topic

confused with Overloading rules  RSS feed

 
Ronwaldo Cruz
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
 
Wouter Oet
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!