wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's Mock #4 Q. 25 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's Mock #4 Q. 25

Maria Garcia
Ranch Hand

Joined: Jul 14, 2002
Posts: 86

Why is the output AAA and not CCC ?


SCJP 1.4
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
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.


Reid - SCJP2 (April 2002)
Tom Adams
Ranch Hand

Joined: Feb 07, 2003
Posts: 56
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)


Tom
Mellihoney Michael
Ranch Hand

Joined: Nov 27, 2002
Posts: 124
polymorphism and overloading,overriding is the key exam point here!!!


a beginner in java
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Dan's Mock #4 Q. 25
 
Similar Threads
overloaded-overriden
overriding + overloading
overloading problem, pls help!
Help with Dan's Chapter 6, exam 1, question 8.
inheritance