tags:

views:

406

answers:

4

Hello,

If both classes are placed in one unit - there is no problem,the child class inherits private methods from the parent class,but if they are in different units,the class can access only public methods.Why?

The child class can't access private methods only because they are in different units.

How do I avoid this? In my case I have 3 child classes and if I place them all in the unit of the parent class,the result will be extremely big.

How do I make a child class that inherits private methods from the parent class in different units?

Thank you!

+2  A: 

You should make them Protected, instead of Private

like so

type 
TMyClass = class(TObject)
Private
   procedure OnlyAccessedViaThisClass;
Protected
   procedure OnlyAccessedViaThisClassOrSubClasses;
Public
   procedure AccessedByAnyone;
end;
Re0sless
+5  A: 

A private method is (unit) private. What you need is a protected method. Protected methods can be accessed by any class that inherits from the base class even if they are in different units. User code can't access them (unless he inherits from the class).

unit A;

interface

type
  TBase = class(TObject)
  private
    procedure PrivateTest;
  protected
    procedure ProtectedTest;
  end;

implementation

procedure TBase.PrivateTest;
begin
end;

procedure TBase.ProtectedTest;
begin
end;

end.

#

unit B;

interface

uses
  A;

type
  TDerived = class(TBase)
  public
    procedure Test;
  end;

implementation

procedure TDerived.Test;
begin
  // PrivateTest; // compile error
  ProtectedTest; // accepted by the compiler
end;

end.

#

unit C;

interface

uses
  A, B;

implementation

var
  Base: TBase;
  Derived: TDerived;

initialization
  Base := TBase.Create;
  Derived := TDerived.Create;

  // Base.PrivateTest; // compile error
  // Base.ProtectedTest; // compile error
  // Derived.PrivateTest; // compile error
  // Derived.ProtectedTest; // compile error
  Derived.Test; // accepted by the compiler

  Derived.Free;
  Base.Free;
end;
Andreas Hausladen
beginning Delphi 2006 there's also a strict modifier (like strict private). With this modifier, the private/protected work like in c++ (strict private - no one outside class can access the method, strict protected - only the class and subclasses can access the method).
Tobias Langner
Tobias, it's Delphi 2005, not 2006.
Rob Kennedy
+1  A: 

Your class can't access those methods because they are private to an ancestor class. You need to read the help files on public, private and protected visibility. Rework your class so those methods are protected if you need to use them in a descendant.

Iron
+3  A: 

This a legacy problem with the scope of class methods. Strictly PRIVATE methods should never have been visible from other classes ever, but in their wisdom Borland made this possible within the same unit, possibly for compatibilitywith FORWARD declarations. Subsequently, much code was created that exploits this capability. To enforce this, Delphi has now introduced STRICT PRIVATE which brings things to how they should always have been without breaking existing code. Bri

Brian Frost