Tony Wolff
Tony Wolff

Reputation: 742

Is there a Delphi equivalent to Java interfaces ("class ... implements")?

Assume I have two classes "mycheckbox" and "mymemo" derived from TCheckBox and TMemo respectively. I want to create a method for both entitled "isdone" which checks to see if the component has been completed. Is there a way of generically accessing that method without knowing which of the two classes one is dealing with?

In Java it would be: class mycheckbox implements MyInterface

Upvotes: 1

Views: 189

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595329

In Delphi, you would use a common interface, just like you would in Java, eg:

type
  IMyInterface = interface
    ['{bfa4fffc-b87e-49ce-8aa9-4911e106959c}']
    function IsDone: Boolean;
  end;

  MyCheckBox = class(TCheckBox, IMyInterface)
  public
    function IsDone: Boolean;
  end;

  MyMemo = class(TMemo, IMyInterface) 
  public
    function IsDone: Boolean;
  end;

function MyCheckBox.IsDone: Boolean;
begin
 Result := ...;
end;

function MyMemo.IsDone: Boolean;
begin
 Result := ...;
end;

procedure DoSomething(Intf: IMyInterface);
begin
 ...
 if Intf.IsDone then...
 ...
end;

Upvotes: 3

Related Questions