PDA

View Full Version : classes vs access



noeska
27-12-2008, 07:01 PM
Delphi/FreePascal
Object Oriented Programming

I have 2 classes. Class 2 is passed to Class 1 as on object. Class 1 then uses procedures in the object (Class 2). Now you can also use Class 2 directly. E.g. create it before passing it on as an object to the Class 1 object. Now i want to hide some procedures when Class 2 is used directly as some procedures only make sense when called from Class 1.
Both are in the same source file. What options do i have to hide some procedures from Class 2 when used directly but be available when called from class 1.

Thanks for your answers in advance.

grudzio
27-12-2008, 08:22 PM
You can make all procedures in class2 that should be available only to class1 private. Private methods and fields can be accessed by any other class in the same unit file.

noeska
27-12-2008, 09:49 PM
I can do that but somehow it does not feel right. But i go use that for now. Thanks.

Brainer
27-12-2008, 10:36 PM
If I understood it correctly, it seems like a perfect usage of the "strict private" declaration range.

noeska
28-12-2008, 08:04 AM
I always assumed that private was to be used in the class where it is declared. And not from the outside not even from the same unit.

Brainer
28-12-2008, 08:18 AM
Private is used to hide methods, properties, and fields from others classes. But there's some kind of "friendship" within a unit, so other classes in the same unit can everything you declared as private. Strict private can make your declarations visible only within the class, ignoring the "friendship" in the unit.