Revision: 69701
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at August 20, 2015 07:24 by xyzeugene
Initial Code
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;
Initial URL
http://stackoverflow.com/questions/1516493/difference-between-strict-private-and-protected-access-modifiers-in-delphi
Initial Description
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.
Initial Title
Delphi/Lazarus: Difference between restrict, private, and protected Access Modifiers
Initial Tags
Initial Language
Delphi