• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance Question - Dan Exam 6 Question 26

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
}}
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene,
Thanks. Your reply was a very clear explanation that cleared up a few nagging details that I apparently did not have straight.
 
reply
    Bookmark Topic Watch Topic
  • New Topic