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 Overloading 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 "Overloading" Watch "Overloading" New topic


Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99

Here o/p is
Eating A
Eating B
Running B

I did not understand how Eating A is displayed.Since they are overloaded methods,The method to be executed is decided at compile time depending on the type of object passed in method.Here I am passing B object reference then how is A displayed.Does it look for type of object on whic it is invoked.
Please explain..

Don Retag

Joined: Aug 15, 2005
Posts: 7
Here is the output lined up:

B b = new B();
A a = new A();
a.eating(b); ==> Eating A
b.eating(b); ==> Eating B
b.running(); ==> Running B

Object 'a' is of type A. Therefore the method os the superclass is classed.
'a' is not of type B.
Pulamathi Pavan Kumar

Joined: May 31, 2005
Posts: 6
Hi Smitha,

The concept u need to understand is:

"When a method is invoked on an object using a reference, it is the
CLASS OF THE CURRENT OBJECT denoted by the reference but not the type of the reference, that determines which method implementation will be executed."

Hope this should suffice u.

Ray Tam

Joined: Mar 01, 2003
Posts: 12
I had a similar question last week.

Here is the link.

I also found questions and answers to questions 4 & 5 on Dan Chisholm's web site very helpful.
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hi Smitha,

Observe this method call -> a.eating(b)

a is of class type this method call is bind to "eating method in class A" at compile time.So even when reference of type B is passed it upcasts an object of type B to A(since A is a superclass) on invoking the method.Remember that compiler will automatically perform the upcasting!!.
Only downcasting need to be taken care by us.
for ex this method call won't work -> b.eating(a);
u need to cast it as b.eating((B)a);

Hope this helps!!

raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60

I have to disagree with these statements.
still works but now the method invoked is that exists in
class A, as 'b' is of type B and it doesn't have eat method
that accepts class type A as argument, but only of type B.
But class B inherits its method eating(typeA) method from
class A.

Also, b.eating((B)a) won't work at runtime, as runtime type of 'a' is
of class type A and not of class type B, this results a runtime exception.

Hope this helps.
Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99

Now In the above example,why the reference type of argument is seem and not reference type of Object on which method is invoked..
Please explain,In overloading when we check refernce type of argument and when reference type of object on which method is invoke..

Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hi Guys,

Sorry for that post.Yes Raghu u r right!!.I haven't tried that sample while posting.

output :

b.eating(a) ==> Eating A

b.eating((B)a) ==> Class cast exception.

To summarize..

a.eating(b) ==> method invoked based on reference type

b.eating(a) ==> method invoked based on arg type

raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60

Let's walkthru the code to make things clear how method resolution works
in this overload scenario.

On Line 1, at compile time, compiler looks at line 1, and interprets that
you are trying to call eating method of class A (as 'a' is reference type A) by passing an argument of class type B.

But class A has eating method which accepts arguments of class A. But from the inheritance structure, B extends A, that means wherever you can pass of type A, a type B class can be passed. So, for line 1, compiler chooses to use eating method from class A and that is the method invoked even at runtime (which version of overload method to be called, is decided at compile time, unlike override methods).

Hope this is clear, if not more confusing, what you already know.
I agree. Here's the link:
subject: Overloading
It's not a secret anymore!