• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overloading

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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..

Cheers
Smitha
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Pavan.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a similar question last week.

Here is the link.

https://coderanch.com/t/250332/java-programmer-SCJP/certification/Overloaded-methods

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

http://www.danchisholm.net/dec20/topic/section6/overload1.html
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Smitha,

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

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);

Hope this helps!!

Regards,
Priya.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.

Hope this helps.
 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

https://coderanch.com/t/250174/java-programmer-SCJP/certification/Overloading

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
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic