/ Published in: Delphi
The difference between private, protected and public is pretty straightforward:
*Private members/methods are only visible within the class that declares them.
*Protected members/methods are visible within the class, and to all subclasses.
*Public members and methods are visible to all other classes.
In Delphi there's a "bug" that makes the visibility of all members public within the same unit. The strict keyword corrects this behaviour, so that private is actually private, even within a single unit. For good encapsulation I would recommend always using the strict keyword.
*Private members/methods are only visible within the class that declares them.
*Protected members/methods are visible within the class, and to all subclasses.
*Public members and methods are visible to all other classes.
In Delphi there's a "bug" that makes the visibility of all members public within the same unit. The strict keyword corrects this behaviour, so that private is actually private, even within a single unit. For good encapsulation I would recommend always using the strict keyword.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
type TFather = class private FPriv : integer; strict private FStrPriv : integer; protected FProt : integer; strict protected FStrProt : integer; public FPublic : integer; end; TSon = class(TFather) public procedure DoStuff; end; TUnrelated = class public procedure DoStuff; end; procedure TSon.DoStuff; begin FProt := 10; // Legal, as it should be. Accessible to descendants. FPriv := 100; // Legal, even though private. This won't work from another unit! FStrictPriv := 10; // <- Compiler Error, FStrictPrivFather is private to TFather FPublic := 100; // Legal, naturally. Public members are accessible from everywhere. end; procedure TUnrelated.DoStuff; var F : TFather; begin F := TFather.Create; try F.FProt := 10; // Legal, but it shouldn't be! F.FStrProt := 100; // <- Compiler error, the strict keyword has "made the protection work" F.FPublic := 100; // Legal, naturally. finally F.Free; end; end;