Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

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