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

  • Home
  • SEARCH
  • 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 621165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:50:41+00:00 2026-05-13T18:50:41+00:00

What is the better approach for separating definition and implementation, using interfaces or abstract

  • 0

What is the better approach for separating definition and implementation, using interfaces or abstract classes?

I actually I don’t like mixing reference counted objects with other objects. I imagine that this can become a nightmare when maintaining large projects.

But sometimes I would need to derive a class from 2 or more classes/interfaces.

What is your experience?

  • 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-05-13T18:50:41+00:00Added an answer on May 13, 2026 at 6:50 pm

    The key to understanding this is to realize that it’s about more than just definition vs. implementation. It’s about different ways of describing the same noun:

    • Class inheritance answers the question: “What kind of object is this?”
    • Interface implementation answers the question: “What can I do with this object?”

    Let’s say you’re modeling a kitchen. (Apologies in advance for the following food analogies, I just got back from lunch…) You have three basic types of utensils – forks, knives and spoons. These all fit under the utensil category, so we’ll model that (I’m omitting some of the boring stuff like backing fields):

    type
        TMaterial = (mtPlastic, mtSteel, mtSilver);
    
        TUtensil = class
        public
            function GetWeight : Integer; virtual; abstract;
            procedure Wash; virtual; // Yes, it's self-cleaning
        published
            property Material : TMaterial read FMaterial write FMaterial;
        end;
    

    This all describes data and functionality common to any utensil – what it’s made of, what it weighs (which depends on the concrete type), etc. But you’ll notice that the abstract class doesn’t really do anything. A TFork and TKnife don’t really have much more in common that you could put in the base class. You can technically Cut with a TFork, but a TSpoon might be a stretch, so how to reflect the fact that only some utensils can do certain things?

    Well, we can start extending the hierarchy, but it gets messy:

    type
        TSharpUtensil = class
        public
            procedure Cut(food : TFood); virtual; abstract;
        end;
    

    That takes care of the sharp ones, but what if we want to group this way instead?

    type
        TLiftingUtensil = class
        public
            procedure Lift(food : TFood); virtual; abstract;
        end;
    

    TFork and TKnife would both fit under TSharpUtensil, but TKnife is pretty lousy for lifting up a piece of chicken. We end up either having to choose one of these hierarchies, or just shove all of this functionality into the general TUtensil and have derived classes simply refuse to implement the methods that make no sense. Design-wise, it’s not a situation we want to find ourselves stuck in.

    Of course the real problem with this is that we’re using inheritance to describe what an object does, not what it is. For the former, we have interfaces. We can clean up this design a lot:

    type
        IPointy = interface
            procedure Pierce(food : TFood);
        end;
    
        IScoop = interface
            procedure Scoop(food : TFood);
        end;
    

    Now we can sort out what the concrete types do:

    type
        TFork = class(TUtensil, IPointy, IScoop)
            ...
        end;
    
        TKnife = class(TUtensil, IPointy)
            ...
        end;
    
        TSpoon = class(TUtensil, IScoop)
            ...
        end;
    
        TSkewer = class(TStick, IPointy)
            ...
        end;
    
        TShovel = class(TGardenTool, IScoop)
            ...
        end;
    

    I think everybody gets the idea. The point (no pun intended) is that we have very fine-grained control over the whole process, and we don’t have to make any tradeoffs. We’re using both inheritance and interfaces here, the choices are not mutually exclusive, it’s just that we only include functionality in the abstract class that’s really, truly common to all derived types.

    Whether or not you choose to use the abstract class or one or more of the interfaces downstream really depends on what you need to do with it:

    type
        TDishwasher = class
            procedure Wash(utensils : Array of TUtensil);
        end;
    

    This makes sense, because only utensils go in the dishwasher, at least in our very limited kitchen which does not include such luxuries as dishes or cups. The TSkewer and TShovel probably don’t go in there, even though they can technically participate in the eating process.

    On the other hand:

    type
        THungryMan = class
            procedure EatChicken(food : TFood; utensil : TUtensil);
        end;
    

    This might not be so good. He can’t eat with just a TKnife (well, not easily). And requiring both a TFork and TKnife doesn’t make sense either; what if it’s a chicken wing?

    This makes a lot more sense:

    type
        THungryMan = class
            procedure EatPudding(food : TFood; scoop : IScoop);
        end;
    

    Now we can give him either the TFork, TSpoon, or TShovel, and he’s happy, but not the TKnife, which is still a utensil but doesn’t really help out here.

    You’ll also notice that the second version is less sensitive to changes in the class hierarchy. If we decide to change TFork to inherit from TWeapon instead, our man’s still happy as long as it still implements IScoop.


    I’ve also sort of glossed over the reference-counting issue here, and I think @Deltics said it best; just because you have that AddRef doesn’t mean you need to do the same thing with it that TInterfacedObject does. Interface reference-counting is sort of an incidental feature, it’s a helpful tool for those times when you need it, but if you’re going to be mixing interface with class semantics (and very often you are), it doesn’t always make sense to use the reference-counting feature as a form of memory-management.

    In fact, I’d go so far as to say that most of the time, you probably don’t want the reference counting semantics. Yes, there, I said it. I always felt that the whole ref-counting thing was just to help support OLE automation and such (IDispatch). Unless you have a good reason to want the automatic destruction of your interface, just forget about it, don’t use TInterfacedObject at all. You can always change it when you need it – that’s the point of using an interface! Think about interfaces from a high-level design point of view, not from the perspective of memory/lifetime management.


    So the moral of the story is:

    • When you require an object to support some particular functionality, try to use an interface.

    • When objects are of the same family and you want them to share common features, inherit from a common base class.

    • And if both situations apply, then use both!

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

Sidebar

Ask A Question

Stats

  • Questions 295k
  • Answers 295k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It is possible to put the Security Token Service class… May 13, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer Look at this statement: x += x--; This is equivalent… May 13, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer It is a bit complicated since your tree has two… May 13, 2026 at 6:50 pm

Related Questions

I have a menu which has an item for each value in an enum.
The case: There is a .net application calling unmanaged C code. Used method for
I am trying to find a better approach for storing people's name in the
I have a bit of code that basically displays the last x (variable, but
I am stuck in a weird Design problem, I am working on a two

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.