Delphi unexpected override behavior

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 and
  • GetMaxim() => Someone created a bomb for us.

The same for the instance of the Heir:

  • GetName() => Heir-Class and
  • GetMaxim() => 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 and
  • GetMaxim() => 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?

+5
source share
2 answers

Inherited gives you not virtual dispatch of the function of the ancestor, and exactly what you observed. Most often, the function from which it is used in the descendant class has the same name as the called inherited function, but, as you have shown, this custom is not required. You can hardly refer to any inherited method from anywhere in the descendant class.

+5
source

This is nothing special. Using inherited you request an implementation of the base class.

The documentation says:

If a member name is inherited, it is a call to the normal method or a reference to a property or field, except that the search for the reference member begins with the immediate ancestor of the application class class

+5
source

Source: https://habr.com/ru/post/1244718/


All Articles