Turn on the time machine and visit ancient times: this is Delphi, and this is 2007.
In our beginner's guide, I write down some basic programming things (with Delphi). Having done this, I found a fad, I do not quite understand. To demonstrate inheritance, I wrote a base class and its descendant. The function is re-introduced, the other is redefined by the heir.
unit UOverride; interface type Base = class(TObject) strict private const CName: String = 'Base-Class'; CMaxim: String = 'Somebody set up us the bomb.'; public function GetName(): String; virtual; function GetMaxim(): String; virtual; end; Heir = class(Base) strict private const CName: String = 'Heir-Class'; CMaxim: String = 'All your Base are belong to us!'; public function GetName(): String; reintroduce; function GetMaxim(): String; override; function GetBaseName(): String; function GetBaseMaxim():String; end; implementation { Base } function Base.GetMaxim: String; begin Result := CMaxim; end; function Base.GetName: String; begin Result := CName; end; { Heir } function Heir.GetMaxim: String; begin Result := CMaxim; end; function Heir.GetName: String; begin Result := CName; end; end.
So, when I create the Base object and call the functions too, it works as expected:
GetName()
=> Base-Class andGetMaxim()
=> Someone created a bomb for us.
The same for the instance of the Heir:
GetName()
=> Heir-Class andGetMaxim()
=> Your whole database belongs to us!
Now I have passed my Heir to Base instance and called both functions again.
GetName()
=> Base-Class, which leaves no questions andGetMaxim()
=> Your whole database belongs to us! as expected, because he is overwhelmed by the Heir.
I added two more features to the Heir out of curiosity.
function Heir.GetBaseName: String; begin Result := inherited GetName(); end; function Heir.GetBaseMaxim: String; begin Result := inherited GetMaxim(); end;
I expect GetBaseName()
give me a Base-Class - and it will. For GetBaseMaxim()
I'm not sure. In my opinion, three things are possible.
- It does not compile or issue an error or
- It starts and the failure is interrupted with an exception (because the inherited function is overridden or
GetMaxim()
from the Heir, telling me that all your Base belongs to us, gives me the majority of the expected ones!
Nothing happens! I force someone to create a bomb. from my base class ?! What for? I thought the function was surpassed by the Heir! It doesnβt matter, but I donβt quite understand this behavior because it doesnβt work as I expected. Any explanations there?
source share