Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance question

 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question from CertPal,


Output is,
Scale Shape

how come the code in line 1 invokes line 3 and not line 2?
 
Cindy Carney
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because scale(long x) is an OVERLOAD of scale(int x), not an override. If the method signatures were the same, it would have printed Scale Circle.

Overloaded methods are chosen at compile time, based on reference variable type. Since you're assigning the Circle instance to a new Shape reference variable, it runs the Shape code.

 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy Carney is right.

If you will try this code



I have just swapped the argument type of "scale" method and passing the long value.

This will not compile because it is overloading not overriding so at compile time compiler will check for a method "scale" with long argument in class "Shap" and generates a compile time error because it will not be able to locate the same.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic