Amazon.com Widgets Accessing Private Stuff with Interfaces

Accessing Private Stuff with Interfaces

By Nick at June 27, 2011 08:38
Filed Under: Delphi, Tech Stuff

I have to admit that the following code surprised me.  I guess it makes sense, but I wouldn’t have guessed that you can expose private functionality via an interface like this.  Interesting. 

program DoPrivateStuff;

{$APPTYPE CONSOLE}

uses
  SysUtils;


type

  ITestInterface = interface
    procedure DoThisPrivateThing;
    procedure DoThisPublicThing;
  end;

  TTestClass = class(TInterfacedObject, ITestInterface)
  private
    procedure DoThisPrivateThing;
  public
    procedure DoThisPublicThing;
  end;

{ TTestClass }

procedure TTestClass.DoThisPublicThing;
begin
  Writeln('Doing a public thing');
end;

procedure TTestClass.DoThisPrivateThing;
begin
  WriteLn('Doing a private thing');
end;

var
   Test: ITestInterface;

begin
  try
    Test := TTestClass.Create;
    Test.DoThisPrivateThing;
    Test.DoThisPublicThing;
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
blog comments powered by Disqus

My Book

A Pithy Quote for You

"Always be a first-rate version of yourself, instead of a second-rate version of somebody else."    –  Judy Garland

Amazon Gift Cards

General Disclaimer

The views I express here are entirely my own and not necessarily those of any other rational person or organization.  However, I strongly recommend that you agree with pretty much everything I say because, well, I'm right.  Most of the time. Except when I'm not, in which case, you shouldn't agree with me.