Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding & overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding & overloading" Watch "overriding & overloading" New topic

overriding & overloading

aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
Hi there,

does the above code uses overloading & overrridding ?

hierachy of inheritance:

A void m1(A a)
B void m1(A a)
^ void m1(B b)

C void m1(A a)
^ void m1(B b)
void m1(C c)

D void m1(A a) //*
void m1(B b)
void m1(C c)
void m1(D d)

when I say d1.m1(a1); at run time D's m1 method is called(which is overriding). but, it takes an argument of type a1. so, the inherited method with the parameter m1(A a) from D * is called( which is overloading).

so, it does overridding & overloading. am i right?

correct me, If my understanding is wrong..

Thanks in Advance

[ August 21, 2007: Message edited by: samura babu ]
[ August 21, 2007: Message edited by: samura babu ]
christian combarel
Ranch Hand

Joined: Aug 04, 2007
Posts: 47
You can't both override AND overload a method.
Overload :
You must change the parameters list of the method (numbers of parameters and/or type). The method to use is chosen at compile time according to the arguments type. It has nothing to do with polymorphism.

Override :
The parameters list must exactly match that of the method from the superclass. However,the return type can be subtype of the return type declared for the method from the superclass. The method to call is decided at runtime according to the object type (and not the reference type).

dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Here you are not overriding any method, you are just performing inheritance. All methods are overloaded methods.

SCJP-1.5<br />SCWCD-1.4
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111

thanks christian combarel & dolly shah!

I expected the output: GFC200.
but, the answer is compile time error.

can any one please explain this?

Thank you

kiran reddy aluguvelli

Joined: Aug 08, 2007
Posts: 7
The answer to the above will be compile time error only but the compile
time error occurs due to the reference m(null).
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

All the three versions of method m() can accept null value. Now when more than one methods are candidate for calling, the compiler looks for the most specific parameter type for the passed value to determine which method to call.
But in this example, compiler found that the method m() with both String and GFC200 are most specific because they both are extending Object, so compiler cannot determine which one to call results an ambiguous method call error.

Hope this helps.

I agree. Here's the link:
subject: overriding & overloading
It's not a secret anymore!