This difference only takes place when we use class of.
Good explanation of it is on the StackOverflow. I realized an example from SO below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
program VirtualConstructor; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils; type TFirst = class constructor Create(); virtual; end; TSecond = class(TFirst) constructor Create(); override; // will shift to shis constructor for class of instances end; TFirstClass = class of TFirst; { TFirst } constructor TFirst.Create; begin Writeln('First'); end; { TSecond } constructor TSecond.Create; begin Writeln('Second'); end; begin try var firstClass := TSecond; var first := firstClass.Create; // will write 'Second' in case of virtual constructor var second := TSecond.Create; Readln; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end. |