Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9034021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:22:46+00:00 2026-06-16T08:22:46+00:00

I work with Delphi 2006 and I have a complex class named TMyClassTest that

  • 0

I work with Delphi 2006 and I have a complex class named TMyClassTest that have many methods
Some of those methods create nonvisual components and assign event handlers of those components and run methods of those components.

Also I have two classes that implement the same interface like below:

TMyClass1 = class(Class1, Interface1)
   ... //procedures from the Interface1
   procedure MyClass1Proc1;
 end;

TMyClass2 = class(Class2, Interface1)
   ... //procedures from the Interface1
   procedure MyClass2Proc1;
   procedure MyClass2Proc2
 end;

Now I need that TMyClass1 and TMyClass2 to ‘inherit’ the TMyClassTest, too.
Much more … Interface1 must contain (beyond its methods) all the methods from the MyClassTest.
How can I avoid to implement (like copy/paste) on both clases (TMyClass1 and TMyClass2) all the procedures from TMyClassTest ?
I don’t want to keep the same code on three separate places.

Based on Arioch’s comments I created a solution like:
(see http://docwiki.embarcadero.com/RADStudio/XE3/en/Implementing_Interfaces#Implementing_Interfaces_by_Delegation_.28Win32_only.29)

    type
      IMyInterface = interface
        procedure P1;
        procedure P2;
      end;
      TMyImplClass = class
        procedure P1;
        procedure P2;
      end;
      TMyClass1 = class(Class1, IMyInterface)
        FMyImplClass: TMyImplClass;
        property MyImplClass: TMyImplClass read FMyImplClass implements IMyInterface;
        procedure IMyInterface.P1 = MyP1;
        procedure MyP1;
      end;
      TMyClass2 = class(TInterfacedObject, IMyInterface)
        FMyImplClass: TMyImplClass;
        property MyImplClass: TMyImplClass read FMyImplClass implements IMyInterface;
        procedure P3;
        procedure P4;
      end;
    procedure TMyImplClass.P1;
         // ...
    procedure TMyImplClass.P2;
         // ...
    procedure TMyClass1.MyP1;
         // ...
    procedure TMyClass2.P3;
         // ...
    procedure TMyClass2.P4;
         // ...
    var
      MyClass: TMyClass1;
      MyInterface: IMyInterface;
    begin
      MyClass := TMyClass1.Create;
      MyClass.FMyImplClass := TMyImplClass.Create; //Error !!!! FMyImplClass is a read only    property !!!
      MyInterface := MyClass;
      MyInterface.P1;   // calls TMyClass1.MyP1;
      MyInterface.P2;   // calls TImplClass.P2;
    end;

Because I have an error at MyClass.FMyImplClass := TMyImplClass.Create; I tried to create FMyImplClass declaring constructor from TMyClass1 and TMyClass2 but don’t work ok.
Is there some other method to create FMyImplClass ?

Now I tried a solution that seem to work ok. Can there happen some hidden efects?

    type
      IMyInterface = interface
        procedure P1;
        procedure P2;
        procedure CreateFMyImplClass;
      end;
      TMyImplClass = class
        procedure P1;
        procedure P2;
      end;
      TMyClass1 = class(Class1, IMyInterface)
        FMyImplClass: TMyImplClass;
        property MyImplClass: TMyImplClass read FMyImplClass implements IMyInterface;
        procedure IMyInterface.P1 = MyP1;
        procedure MyP1;
        procedure CreateFMyImplClass;
      end;
      TMyClass2 = class(TInterfacedObject, IMyInterface)
        FMyImplClass: TMyImplClass;
        property MyImplClass: TMyImplClass read FMyImplClass implements IMyInterface;
        procedure P3;
        procedure P4;
        procedure CreateFMyImplClass;
      end;
    procedure TMyImplClass.P1;
         // ...
    procedure TMyImplClass.P2;
         // ...
    procedure TMyClass1.MyP1;
         // ...
    procedure TMyClass1.CreateFMyImplClass;
    begin
     FMyImplClass := TMyImplClass.Create;
    end;
    procedure TMyClass2.P3;
         // ...
    procedure TMyClass2.P4;
         // ...
    procedure TMyClass2.CreateFMyImplClass;
    begin
     FMyImplClass := TMyImplClass.Create;
    end;
    var
      MyInterface: IMyInterface;
    begin
      if WantRemote then
         MyInterface := TMyClass1.Create
      else
         MyInterface := TMyClass2.Create;
      MyInterface.CreateFMyImplClass;   // create FMyImplClass ;
      MyInterface.P2;   // calls TImplClass.P2;
    end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:22:47+00:00Added an answer on June 16, 2026 at 8:22 am

    Delphi does not have Scala-like traits or Python-like mixins, nor it support multiple inheritance a la C++.

    If you cannot make Class1 and Class2 inherit from TMyClassTest, then perhaps you have to rely on interface delegation: make TMyClassX no more implementing Interface1 directly, but instead add them a field of TMyClassTest and delegate their Interface1 to this field.

    I think you’d better

    1. move those new common functions into some Interface0 type
    2. make Interface1 inherited from Interface0
    3. make some TMyClassesBaseCommonTrait class, implementing Interface0
    4. make two subclasses TMyClass1InternalEngine(TMyClassesBaseCommonTrait) and TMyClass2InternalEngine(TMyClassesBaseCommonTrait) implementing (in different, TMyClassX-specific ways, the rest of Interface1(Interface0) API
    5. have TMyClassX classes internal private field of TMyClass2InternalEngine type doign real implemntation

    Google for “delphi interface delegation” shows this as top link: Delphi: How delegate interface implementation to child object?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some customers/candidate who complained that my program doesn't work on their Windows
I have been experimenting with TMask in Delphi 2010 and it seems to work
Is there any work-around to create mutual referencing records in Delphi? Here's the simplified
I'm doing some Delphi (2010) work this summer, and I've stumbled upon this problem:
Hi I have been using this function in Delphi 2006, but now with D2010
I use Delphi 2007 in my daily work and have now start to look
I have an Oracle 9 database from which my Delphi 2006 application reads data
I have a Delphi (BDS 2006) application which sends keystrokes to QuickBooks accounting software
I need an encryption library (VCL or not) that will work with Delphi XE2
once again conversion problem in Delphi. I've been through some other topics, that refer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.