Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan's Mock #4 Q. 25

 
Maria Garcia
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why is the output AAA and not CCC ?
 
Reid M. Pinchback
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short answer is "because this is Java, not C++".
The long answer is that you have to keep track of what type of reference you are using at any given time.
Consider the line "c1.m1(c4)".
c4 is a C reference.
c1 is an A reference.
A objs have method m1 that takes an A argument
c4 gets converted into an A reference
the c1 method gets invoked
This isn't like C++ where you invoke according to the most resolved type, one of those subtle language differences. If class A had an overloaded method that also took a C reference as an arg, then you'd get the behaviour you expected. Resolution is done in terms of information available at compile-time, and so the definition of A (the reference type) determines what gets invoked.
Much the same argument for the other two statements.
 
Tom Adams
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my take on it...
In the C class m1 is an overloaded method not an overridden method. Overrides must have the exact same signature as the original. Thus the call from the A object reference call can only invoke the method in class A.
If the m1 method in class C was called p1 it wouldn't be confusing...but with a different signature it might as well have a different name.
Any method call from the A object reference not defined within that class = you can't get there from here.
Alternate version of the class with comments...took out some of the stuff that didn't seem? to matter.

"...the reference type (not the object type) determines which overloaded method is invoked"
- Sun Certified Programmer & Developer for Java 2 Study Guide (Exam 310-035 & 310-027)
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
polymorphism and overloading,overriding is the key exam point here!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic