|A demo of virtual methods|
On this page we will make two artificial classes with the purpose of demonstrating virtual methods
The method res sends the message get-state to itself. If an x object receives the message res , it will return the number 1.
The y class redefines the method get-state . The y class inherits the method res . If a y method receives the message res , it will be propagate to the x part of the object. Due to the use of virtual methods, self in the x part refers to the y part. Therefore the get-state message returns the y-state of the y part, namely 2.
Instances of the classes x and y are created and res messages are send to both of them.
The crucial observation is that the y object bound to the variable b returns the number 2 when we send it the res message.