File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inheritance question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inheritance question " Watch "inheritance question " New topic

inheritance question

Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
Question from CertPal,

Output is,
Scale Shape

how come the code in line 1 invokes line 3 and not line 2?

SCJP 6.0 90%
Cindy Carney
Ranch Hand

Joined: Jan 12, 2010
Posts: 57
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

Joined: Jan 14, 2010
Posts: 317

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.
I agree. Here's the link:
subject: inheritance question
jQuery in Action, 3rd edition