[323] in java-interest
confusion over which method is invoked
daemon@ATHENA.MIT.EDU (Steve Drach)
Wed Jun 14 21:18:31 1995
Date: Wed, 14 Jun 1995 17:47:31 -0800 (PDT)
From: Steve Drach <Steve.Drach@Eng.Sun.COM>
Reply-To: Steve Drach <Steve.Drach@Eng.Sun.COM>
To: java-interest@java.Eng.Sun.COM
I'm a little confused about the output from the following program. It
seems to me that since ClassA.printname is not an abstract method,
then it should be called by the a.printname() statement in the main
method. Instead the printname method in ClassB is called. I really
don't understand why. The same program in C++ seems to work the way I
expect.
class ClassA {
String name = "ClassA";
void printname() {
System.out.println("A: " + name);
}
}
class ClassB extends ClassA {
String name = "ClassB";
void printname() {
System.out.println("B: " + name);
}
}
class AccessTest {
public static void main(String argv[]) {
ClassB b = new ClassB();
ClassA a = b;
a.printname();
System.out.println(a.name);
}
}
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com