To make a method virtual or dynamic, include the virtual or dynamic directive in its declaration. Virtual and dynamic methods can be overridden in descendent classes. When an overridden method is called, the actual (run-time) type of the class or object used in the method call--not the declared type of the variable--determines which implementation to activate.
To override a method, redeclare it with the override directive. An override declaration must match the ancestor declaration in the order and type of its parameters and in its result type (if any).
In the following example, the Draw method declared in TFigure is overridden in two descendent classes:
type TFigure = class procedure Draw; virtual; end; TRectangle = class(TFigure) procedure Draw; override; end; TEllipse = class(TFigure) procedure Draw; override; end;
procedure ExecuteDraw(fig: TFigure)
ReplyDeletebegin
// based on the object passed in, appropriate Draw method gets called
fig.Draw()
end;