Reasoning behind the inability to override a property

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Reasoning behind the inability to override a property

leledumbo
Administrator
Just my curiosity about this particular "feature", why is property overriding not allowed (redeclaring with different visibility is however allowed)? e.g. consider the following code fragment:

type
  TA = class
  private
    FP: Integer;
  public
    property P: Integer read FP;
  end;

  TB = class(TA)
  private
    FP: String;
  public
    property P: String read FP;
  end;

Because TB inherits TA, it can't declare a property named P since TA already has it.
The real world code having this "problem" is fpWeb:

TFPWebModule = Class(TCustomFPWebModule)
  ...
end;

TCustomFPWebModule = Class(TSessionHTTPModule)
Public
  ...
  Property Actions : TFPWebActions Read FActions Write SetActions;
  ...
end;

TFPWebActions = Class(TCustomWebActions)
  ...
end;

TFPWebAction = Class(TCustomWebAction)
  ...
end;

TCustomWebActions = Class(TCollection)
public
  ...
  Property Actions[Index : Integer] : TCustomWebAction Read GetActions Write SetActions; Default;
  ...
end;

TCustomWebAction = Class(TCollectionItem)
  ...
end;

The "problem" lies in the Actions property of TCustomWebActions, which TFPWebActions cannot override, and therefore always returns a TCustomWebAction, even though the actual type is TFPWebAction, as that's how it's created:

constructor TCustomFPWebModule.CreateNew(AOwner: TComponent; CreateMode : Integer);
begin
  ...
  FActions:=TFPWebActions.Create(TFPWebAction);
  ...
end;

constructor TCustomWebActions.Create(AItemClass: TCollectionItemClass);
begin
  inherited Create(AItemClass);
  ...
end;

This forces the use of typecast when accessing specific action through TFPWebModule.Actions. The same thing applies to TCustomWebActions.Add which TFPWebActions doesn't override (it returns TCustomWebAction instead of TFPWebAction), but can be solved by overriding, just like how TCustomWebActions.Add overrides TCollection.Add. Hence, only the property overriding problem left.