aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Question - Dan Exam 6 Question 26 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 "Inheritance Question - Dan Exam 6 Question 26" Watch "Inheritance Question - Dan Exam 6 Question 26" New topic
Author

Inheritance Question - Dan Exam 6 Question 26

Don Wood
Ranch Hand

Joined: Dec 05, 2003
Posts: 65
I thought I understood the rules on which overriding method is called until I came across this question. I have expanded the original question to explain where my confusion is. When I run this, the output is ACC. I expected the output to be CCC. So I am confused about why method m1 in class A is called instead of m1 in class C.
I thought the relevant rule was that since the methods are not static and not private, the type of the object (not the type of the reference) would control where the java runtime would begin its search for a matching method.
In other words, c1 is a type 'A' but the object is actually a type 'C' so I expected the call to go to method m1 in class C. The call to methods m2 and m3 seem to be using this rule but the call to m1 uses another rule that I don't understand.
class A {
void m1(A a) {System.out.print("A");}
void m2() {System.out.print("A");}
void m3(String S) {System.out.print("A");}
}
class B extends A {
void m1(B b) {System.out.print("B");}
void m2() {System.out.print("B");}
void m3(String S) {System.out.print("B");}
}
class C extends B {
void m1(C c) {System.out.print("C");}
void m2() {System.out.print("C");}
void m3(String S) {System.out.print("C");}
}
class D {
public static void main(String[] args) {
A c1 = new C();
C c2 = new C();
c1.m1(c2);
c1.m2();
c1.m3("hello");
}}
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Hi Don. Welcome to the JavaRanch.
This example is about overloading, not overriding.
c1.m1(c2)
At compile-time, the compiler knows the type of the variable c1 is A. There is only one method named m1 declared in or inherited by class A. The compiler chooses m1(A a) in class A.
At run-time, the Java virtual machine knows the actual type of the object referenced by c1 is C. The VM looks for an overriding method m1(A a) in C. The method m1 in C does not override the method in A, because the parameter types are not the same. The VM uses the method m(A a) in class A.
At compile-time, resolving a method name may involve choosing an overloaded method. At run-time, invoking a method may involve choosing an overriding method.
Marlene
[ December 05, 2003: Message edited by: Marlene Miller ]
Don Wood
Ranch Hand

Joined: Dec 05, 2003
Posts: 65
Marlene,
Thanks. Your reply was a very clear explanation that cleared up a few nagging details that I apparently did not have straight.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inheritance Question - Dan Exam 6 Question 26