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.
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.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
posted 9 years ago
Thanks. This answered my question
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database