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 8030149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:40:27+00:00 2026-06-05T00:40:27+00:00

When we design a class in Delphi, typically we have private fields (members), private

  • 0

When we design a class in Delphi, typically we have private fields (members), private setter and getter methods and a public property. From outside the class, the access of that data is made only by the public property; the users of the class don’t even know that a getter method exists.

So the getter and setter methods encapsulate the instance member and the property encapsulates the getter and setter methods.

However, when defining an interface we are exposing those methods:

ICounter = interface
  // I wouldn't want to specify these 2 methods in the interface, but I'm forced to
  function GetCount: Integer;
  procedure SetCount(Value: Integer);

  property Count: Integer read GetCount write SetCount;
end;

Implementing the concrete class:

TCounter = class(TInterfacedObject, ICounter)
private
  function GetCount: Integer;
  procedure SetCount(Value: Integer);
public
  property Count: Integer read GetCount write SetCount;
end

Using it:

var
  Counter: ICounter;
begin
  Counter := TCounter.Create;
  Counter.Count := 0; // Ok, that's my public property

  // The access should me made by the property, not by these methods
  Counter.SetCount(Counter.GetCount + 1);
end;

If the properties encapsulate the getter/setter private methods, isn’t this a violation? The getter and setter are the internals of the concrete class and shoudn’t be exposed.

  • 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-05T00:40:29+00:00Added an answer on June 5, 2026 at 12:40 am

    Methods are the primary way of interacting with an interface. Properties on interfaces are a Delphi-specific extension; they just provide syntactic sugar for the underlying methods. No other language Since methods in interfaces are by definition public, they are not encapsulated by properties. You’re not revealing any implementation details by showing that a property is backed by methods because in interfaces, properties are always backed by methods, and methods are always public. Encapsulation cannot be violated if it was never present in the first place.

    Your example concrete class is misleading. First, the property defined there has absolutely no connection to the property defined in the interface. You could define it as read-only, make it directly access data members, make it private, or differ from the interface version in any other way, including remove it entirely, and it would have no effect on users of the interface, lending further credence to the notion that it’s the methods that matter in an interface, not the properties. The compiler translates any use of the interface property directly into use of one of the corresponding interface methods, which are already public. The implementing object is never consulted on the matter.

    Second, the visibility specifiers on the class are irrelevant. There’s no need to make the methods private since they’re public on the interface already. However, making them private isn’t a bad idea since it encourages proper use of the class via the interface.

    You could complain that the accessor methods of an interface should be able to be private, but that’s the same as asking for interface methods in general to be able to be private, and that makes no sense. A method that can’t be called obviously isn’t part of the interface. Recall that interfaces can be consumed by any COM-supporting language, even ones that have no notion of properties, like C and C++. Those languages need to be able to call the accessor methods, too. If the methods were somehow private, the interface wouldn’t work in those languages.


    When a property of a Delphi class refers to a field, that detail is actually part of the class’s public-facing interface. Any code that uses that property knows that the property is just an alias for the field (even if the author of the code didn’t know that). If you change the property definition, any code using that class needs to be recompiled so the compiler can generate new code for accessing the property.

    When properties are required to be backed by methods, you can’t really change the property definition anymore. Only the implementation can change, and so no consumers of the interface need recompiling just because you choose to make a property be calculated on demand instead of read from a stored field.

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

Sidebar

Related Questions

In my homework, I have to design a class Message; among other attributes, it
I have problem with class design. I have core class for my game objects.
I'm taking an operating systems design class in which they have given us a
I have a question regarding class design. I want to have a class that
So I have to design a class that works on a collection of paired
I design a class: public class CustomEvent< P, T > { /** Facade interface
I have a class design problem that could simplified with this example: // foo.h
I will explain my class design public class A { public Ilist<B> Something {
Should a design class diagram show private operations? I guess that a design class
I'm taking a compiler-design class where we have to implement our own compiler (using

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.