Originally Posted by clivew
If you take a look at the the source code for classes declared and implemented in
Delphi's own units (e.g. classes) you will see plenty of examples of using the
private, protected and public sections of a class.
Funny advice. Yes, they will provide you examples of using private, protected, and public sections of a class. But this is irrelevant to beginners, since they did not explain the reason why they were placed in their respected section
Originally Posted by llaama2012
.... I am still to see a program with methods declared in the public or private sections. Also, I have read that there are protected and public sections for some (most?) classes, but I am not sure I would know what they are and why they are protected...
This is an aspect/feature of Object Oriented Programming calles Encapsulation
. Simply said, you want to control access to properties and methods of your classes by using the sections in order for them to be used properly.
Basically there are 3 kind of access scope of a class.
Put everything you want to be accessible only from the same class or codes in the same unit in this section. Even descendant of the class could not access things in the private section (unless declared in the same unit).
Things in this section are accessible by descendant of the class (or codes in the same unit). So, put everything you want to be accessible from the descendant of the class here.
Things here is accessible to anything that has access to the class. So put everything you want to be accessible by anything that has access to the class here.
Now there are special cases of these sections.
This actually the same with public. However things in this section could be accessible in design time (e.g. through Object Inspector), since Delphi will automatically generate RTTI (RunTime Type Information) for them. RTTI is making classes accessible in design time. There is some conditions for generating RTTI, but I will not talk too much details here.
Also published properties are storable in .dfm file.
This section will make things inside it accessible only through methods of the same class. No codes outside the class, even if in the same unit, can access things inside strict private section.
This section will make things inside it only accessible through methods of the same class and its descendant. No codes outside the class or its descendant, even codes in the same unit, can access things inside strict protected section.
If you declare values, properties, or methods in a class without specifically say what their scope is, like this:
They will either become public or published. If the class is a descendant of TComponent, they will become published. Otherwise the will become public.