overload,override,virtual keyword

Posted by wannalearnit on Sat, 22 Jan 2022 05:39:04 +0100

overload polymorphism

In pascal syntax rules, there cannot be two functions with the same name in the same UNIT.

For example:

function func(): Boolean;
function func(const x: Char): Boolean;

In order to solve this problem, the overload keyword is introduced.

As follows:

function func(): Boolean; overload;
function func(const x: Char): Boolean; overload;

Override override

When deriving a new class, it will first inherit all private member variables and functions in the parent class.

If we want to add new functions to a method of a derived class instead of using the functions of the original method of the parent class, we can override the parent class method with override. In this method, you can use inherited to call a method with the same name in the parent class.

example:

 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
 7   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
 8 
 9 type
10   TBaseClass = class(TObject)
11     public
12       procedure MessageBox() ;
13       procedure vMsg(); virtual;
14   end;
15   TSonClass = class(TBaseClass)
16     public
17       procedure MessageBox();
18       procedure vMsg(); override;
19   end;
20 
21   TForm1 = class(TForm)
22     Button1: TButton;
23     Button2: TButton;
24     procedure Button1Click(Sender: TObject);
25     procedure Button2Click(Sender: TObject);
26   private
27     { Private declarations }
28   public
29     { Public declarations }
30   end;
31 
32 var
33   Form1: TForm1;
34 
35 implementation
36 
37 {$R *.dfm}
38 
39 { TBaseClass }
40 
41 procedure TBaseClass.MessageBox;
42 begin
43   MessageDlg('TBaseClass', mtInformation, [mbOK], 1);
44 end;
45 
46 procedure TBaseClass.vMsg;
47 begin
48   MessageDlg('TBaseClass vvv', mtInformation, [mbOK], 1);
49 end;
50 
51 { TSonClass }
52 
53 procedure TSonClass.MessageBox;
54 begin
55   MessageDlg('TSonClass', mtInformation, [mbOK], 1);
56 end;
57 
58 procedure TSonClass.vMsg;
59 begin
60   MessageDlg('TSonClass vvv', mtInformation, [mbOK], 1);
61 end;
62 
63 procedure TForm1.Button1Click(Sender: TObject);
64 var
65   aObj : TBaseClass;
66 begin
67   aObj := TBaseClass.Create;
68   aObj.MessageBox;
69   aObj.vMsg;
70   aObj.Free;
71 end;
72 
73 procedure TForm1.Button2Click(Sender: TObject);
74 var
75   aObj : TBaseClass;
76 begin
77   aObj := TSonClass.Create;
78   aObj.MessageBox;  //Because the method of the parent class is not virtual,Therefore, the method of the parent class is called
79   aObj.vMsg;        //Because the method of the parent class is virtual,Therefore, the method of the subclass is called
80   aObj.Free;
81 end;
82 
83 
84 end.

Virtual virtual function (the following paragraph should be understood in depth)

Virtual keyword specifies that the method is a virtual method. In Object Pascal, by default, all methods are static (different from class methods, of course). The characteristic of static methods is that when the object is created, the compiler will allocate all the memory addresses specified when calling static methods, that is, the specific methods to be called are specified during compilation.


Virtual and dynamic methods (virtual and dynamic methods) are dynamically allocated at call time. Their semantics are the same, the only difference is their implementation method and calling method, which involves the compilation mechanism of DELPHI.

Delphi's compiler will automatically maintain the virtual method table (VMT) for adding virtual methods and the dynamic method table (DMT) for adding dynamic methods.
The virtual method table stores all virtual method pointers declared by the class and its base class. Each class has and has only one virtual method table. Of course, each virtual method table has an entry. Whether a class has its own virtual methods or not, as long as it inherits the virtual methods of its ancestor class, it will also have its own virtual method table and list all the virtual methods it inherits. Because each class has its own virtual method table, Delphi can use the virtual method table to identify a class. In fact, a class reference is a pointer to the virtual method table of the class.

Abstract method - abstract method

Abstract method is what we often call an abstract method. This method can only have a declaration but not an implementation part. It is generally provided for the derived class override. Therefore, the general abstract keyword and virtual appear together. Generally, the class containing abstract methods is called virtual class.