Double Inheritance and Abstract method
-
25-06-2021 - |
Question
I have a base that is inherited twice. The second subclass doesn't provide any signature for the method clone, but the third subclass does and defines the method clone as follows.
TControlObject = abstract class
...
public
method Clone:TControlObject; virtual; abstract;
end;
TGateControl = class(TControlObject)
...
public
...
end;
TAndControl = class(TGateControl)
public
method Clone:TControlObject; override;
end;
However, compiler raises an error that TGateControl class doesn't provide implementation for clone method. Since TGateControl is inherited from TControlObject and TAndControl class is inherited from TGateControl, the method clone should automatically be overridden for the base class clone method. Am I right?
Thanks in advance,
La solution
You have the abstract
attribute on the Clone method in TControlObject
. This means that any class that directly derives from TControlObject
must provide an implementation of the Clone method (see MSDN abstract). As a result TGateControl must provide an implementation of Clone. If TControlObject
had a concrete implementation of Clone then, yes, it would not need to be overridden.
So some options are to:
- Either remove the
abstract
attribute on clone. - Add an implementation of
Clone
in eitherTControlObject
orTGateControl
.
And to clarify, these methods are never "automatically overridden". The derived class is able to call the base class' implementation but it would be incorrect to say that the derived class has "automatically overridden" the base class' implementation.