• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance Qs.  RSS feed

 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gurus,

Could you please explain me why the following code is printing "Scale Shape" instead of "Scale Circle"




Thanks in advance.



Regards

Suresh.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, note that the method in class Circle doesn't override the one in class Shape because the argument types are different. Now note that the reference variable "s" is of type Shape; the only scale() method the compiler knows about that can be called through this reference is the one with the "long" argument that's defined in class Shape.

If Circle.scale() were change to accept a long argument, or Shape.scale() to accept an int, then this program would print "Scale Cirle".
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!