Hi Alex and other Ranchers,
When using polymorphism, the methods of the child are called, but the members of the parent are used (see "Polymorphism access").
I have tried the example you sent and saw that when using
polymorphic method calls, the methods of the subclass "class B" are
called, but the members of the superclass "class A" are called.
This is interesting and I'm asking myself Why is it calling the members of the superclass instead of the subclass as one would normally assume.
The reason I can think of is that when you call a method (polymorphic) on an object, also the implicit parameter
------------------------------------------------
System.out.println("\nPolymorphism access:");
A ab = new B();
ab.doStuff(); // polymorphic method call - passes an implicit parameter
// doStuff() in B called.
------------------------------------------------
(pointing to the SubClass object in the heap memory) is passed which then resolves to the correct method
to be executed through "Dynamic Binding". (i.e. doStuff in class B is called)
------------------------------------------------
class B extends A {
public char var = 'B';
B doStuff() {
...;
}
}
------------------------------------------------
Whereas when you access the public member variable of the Superclass - there is No Dynamic binding mechanism
to resolve the correct member variable to be called.
Can anyone Please correct me if I'm wrong or Confirm if this is what happens?
Regards,
Siphiwe Madi
[ January 22, 2008: Message edited by: Siphiwe Madi ]