• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Overloading

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

Answer:b. explanation for this answer is that the reference variable is NOT used for selection but the reference Argument is used.

Answer: a.Explanation for this answer is that the reference variable c1 is used to determine which m1() will be called. Since c1 is of type A therefore A's m1() is called.

My question is why does it check the reference argument in the first case and the reference variable in the second. What am i missing ??
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Murtuza,





Overloaded methods go by reference type at run-time.

In this case the methods are overloaded. when c4.m1(c1) is executed it goes to m1() method of class C(ref type), but since the arguments does not match it goes to immediate superclass(since its overloaded) and checks whether it match's and so on. When it matchs class A's m1() method it prints A. Similarly B. For c4.m1(c3) it matchs exactly so C. so the answer ABC.




[\QUOTE]

As said before overloaded goes by reference type. So the statement
A c1 = new C(); //
Here the ref. type is A and so goes directly into class A and executes m1() and prints A. Also note here that the subclass argument(c2) is casted implicitly to superclass type.

Hope that clears your doubt.
Kits

 
Murtuza Akhtari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kitty....
that was helpful !!
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a sample chapter from my book that covers method overloading.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic