This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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..
"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."
a is of class type A..so 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);
I have to disagree with these statements. b.eating(a) 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.
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..
Thanks.. Smitha
Priya Jothi
Ranch Hand
Joined: Jul 13, 2004
Posts: 168
posted
0
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
Thanks, Priya.
raghu babu
Ranch Hand
Joined: Jul 05, 2005
Posts: 60
posted
0
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.