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 overload, override 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 "overload, override" Watch "overload, override" New topic

overload, override

Denes Doma

Joined: Jun 18, 2003
Posts: 5
I have a question.

In this case b will see E.amethod, and call it.

In this case b will NOT see E.amethod, and call Base.amethod.

I know that the difference is 1. is override, and 2. is overload, but I don't understand WHY it works like this.
Pls. help.
Edited by Corey McGlone: Added CODE tags
[ June 23, 2003: Message edited by: Corey McGlone ]
Lawrence Chettiar
Ranch Hand

Joined: Apr 10, 2003
Posts: 62
Well the answer lies not in overloading or overridding but in inhertance
As you know in the example when u call b.<anything> the scope of the compiler is only for the methods in Base as compiler see that the parameter is int which can be promoted to double (ref JLS). It will bind b.amethod(1) call to amethod(double) base method instead of int.
One important point remember since u overload the amethod(int) in subclass b.amethod() cannot see this new method at all (inhertance rule).
Hope this helps.

SCJP 1.4 (86%)<br />SCMAD Beta (77%)<br />SCEA (part I:89%,partII:93%)<br />MCAD (cleared 70-315 & 70-320 [Score 980])
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
In the first scenario, the two methods have the exact same signature. This creates an overridden method and, hence, the JVM uses dynamic binding to determine which method should be invoked at runtime.
In the second scenario, the two methods have different signatures (one takes a double while the other takes an int) so we have what is known as method overloading (multiple methods with the same "name" but different signatures). In this case, the compiler can determine which method should be invoked at compile time.
Be sure to check out these sections of the JLS for more info:
§8.4.6 Inheritance, Overriding, and Hiding
§8.4.7 Overloading
I hope that helps,
[ June 23, 2003: Message edited by: Corey McGlone ]

SCJP Tipline, etc.
Joe Brennan

Joined: Jun 05, 2003
Posts: 9
In case 1 , the complier knows that amethod is overridden , so leaves the decision of which method to call until runtime.
In case 2 , as amethod is overloaded , the complier will try to match up the method based based on the parameters of the reference type.
A match is possible ( 1 can fit in a double ).
If this was not possible you'd get a compile time error.
I'm not sure why it doesn't use the actual object type at run-time.

SCJP 1.4<br />MCP
I agree. Here's the link:
subject: overload, override
It's not a secret anymore!