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,

Était-ce utile?

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:

  1. Either remove the abstract attribute on clone.
  2. Add an implementation of Clone in either TControlObject or TGateControl.

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top