GMEFil
GMEFil

Reputation: 137

Abstract error when extending classes

type
  TObjA = class
    a: string;
  end;
type
  worker<T> = interface
    function step1(v: integer): T;
    function step2(s: string): T;
  end;

type
  ImplA<T> = class(TInterfacedObject, worker<T>)
    function step1(v: integer): T;
    function step2(s: string): T; virtual; abstract;
  end;

type
  ImplB = class(ImplA<TObjA>)
    function step2(s: string): TObjA;
  end;
implementation


function ImplA<T>.step1(v: integer): T;
begin
  result := step2(IntToStr(v));
end;

function ImplB.step2(s: string): TObjA;
var
  r: TObjA;
begin
  r := TObjA.Create;
  r.a := 'step2 ' + s;
  result := r;
end;

I am trying to build a functionality according to this structure. I know it works in java, but currently I am working in delphi 2010. I get an abstract error when calling ImplB.step1(1) How do I fix this?

Upvotes: 1

Views: 365

Answers (1)

Re0sless
Re0sless

Reputation: 10886

You get the error as you do not declare function step2(s: string): TObjA; as an override.

So in

function ImplA<T>.step1(v: integer): T;
begin
  result := step2(IntToStr(v));
end;

it is calling step2 from ImplA not ImplB as you are expecting it to

Your also changing the return type from a generic object to TObjA, the compiler may not like that, but I don't have a copy of Delphi that supports generics to hand to test.

Upvotes: 5

Related Questions