Overridden Members
Subclass objects are also legal representative objects of their parent classes. For example, every Linked-Packet object is a perfectly legal Packet object.
The handle of a LinkedPacket object can be assigned to a Packet variable:
LinkedPacket lp = new;
Packet p = lp;
In this case, references to p access the methods and class properties of the Packet class. So, for example, if class properties and methods in LinkedPacket are overridden, these overridden members referred to through p get the original members in the Packet class. From p, new and all overridden members in LinkedPacket are now hidden.
class Packet;integer i = 1;
function integer get();
get = i;endfunctionendclass
class LinkedPacket extends Packet;
integer i = 2;
function integer get();
get = -i;endfunctionendclass
LinkedPacket lp = new;
Packet p = lp;
j = p.i; // j = 1, not 2
j = p.get(); // j = 1, not -1 or –2
No comments:
Post a Comment