Class: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Class
| Old | New | Line | |
|---|---|---|---|
| 1 | + | A **class** is a highly structured data [[Type|type]] in Object [[Pascal]] dialects such as [[Mode Delphi|Delphi]] or the [[Mode ObjFPC|ObjFPC]] dialect. Classes are able to contain [[Variable|variables]], [[Constructor|constructors]], [[Destructor|destructors]], [[Function|functions]], [[Procedure|procedures]], and [[Property|properties]] using access scopes. | |
| 2 | + | ||
| 3 | + | Classes are able to inherit and to be inherited by other classes. For [[runtime|run-time]] purposes, any class not specifying a parent class automatically inherits from [[TObject]], as it has required components for all classes. Because of TObject's dependency, any [[subclass]]'s destructor must have the [[override]] directive. Additionally, any of your class's constructors must specify [[Inherited|inherited]] in their body. A class can have several constructors, but only one destructor. | |
| 4 | + | ||
| 5 | + | Object Pascal does not support multiple inheritance: apart from the implicit inheritance by TObject, classes can have only one ancestor class. Polymorphism is implemented with method directives. Here is a simple class declaration; let's explain it. | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | type | |
| 9 | + | TMyClass = class | |
| 10 | + | private | |
| 11 | + | FSomeVar: integer; | |
| 12 | + | public | |
| 13 | + | constructor Create; overload; | |
| 14 | + | constructor Create(Args: array of integer); overload; | |
| 15 | + | destructor Destroy; override; | |
| 16 | + | function GetSomeVar: integer; | |
| 17 | + | procedure SetSomeVar(newvalue: integer); | |
| 18 | + | published | |
| 19 | + | property SomeVar: integer read GetSomeVar write SetSomeVar default 0; | |
| 20 | + | end; | |
| 21 | + | ``` | |
| 22 | + | ||
| 23 | + | Between the keywords 'class' and 'end' we find member declarations, both variables and methods. Some methods (functions and/or procedures) are preceded by scope modifiers (**private**, **public**, **published**) and followed by directives (**overload**, **override**) and a strange **property** thing. Let's explain them all. | |
| 24 | + | ||
| 25 | + | ## Notes on inheritance | |
| 26 | + | ||
| 27 | + | In Object Pascal, derived classes inherit all members of the base class, even those not overloaded with the same name. Example: | |
| 28 | + | ||
| 29 | + | ``` pascal | |
| 30 | + | type | |
| 31 | + | //base class | |
| 32 | + | MyClass = class | |
| 33 | + | procedure Proc1; | |
| 34 | + | end; | |
| 35 | + | ||
| 36 | + | //derived class | |
| 37 | + | YourClass = class(MyClass) | |
| 38 | + | procedure Proc1; //same name of MyClass procedure | |
| 39 | + | end; | |
| 40 | + | ||
| 41 | + | var | |
| 42 | + | a: MyClass; | |
| 43 | + | b: YourClass; | |
| 44 | + | begin | |
| 45 | + | a := MyClass.Create; | |
| 46 | + | b := YourClass.Create; | |
| 47 | + | a.Proc1; // uses procedure in MyClass | |
| 48 | + | b.Proc1; // uses procedure in YourClass | |
| 49 | + | MyClass(b).Proc1; // uses procedure in MyClass | |
| 50 | + | ``` | |
| 51 | + | ||
| 52 | + | ## Scope modifiers | |
| 53 | + | ||
| 54 | + | Scope modifiers tell the compiler who can call a method: | |
| 55 | + | ||
| 56 | + | - private: the member can be called/accessed only by other methods in the same class; | |
| 57 | + | - public: the member can be called/accessed by any other part of the program; | |
| 58 | + | - protected: the member can be called/accessed from other classes in the same unit and from derived classes, but not from further derivated classes. | |
| 59 | + | - published: the variable is public, and it will appear in the IDE's Object Inspector. | |
| 60 | + | ||
| 61 | + | Scope modifiers cannot be changed in derived classes: members will maintain their visibility (or lack thereof) forever, everywhere. | |
| 62 | + | ||
| 63 | + | ## Properties | |
| 64 | + | ||
| 65 | + | A property is a variable that is accessed through methods. The variable SomeVar in the example above will be read and written in the code like a simple variable, but under the hood the [[Compiler|compiler]] will call the methods specified in the property declaration. This allows to calculate it on the fly, or run boundary checks, input validation, formatting or just a placeholder for future extensions. Some rules apply: | |
| 66 | + | ||
| 67 | + | - Either read or write methods, at least one of them, must be present; | |
| 68 | + | - The read method (if present) cannot have parameters; | |
| 69 | + | - The write method (if present) must have exactly one parameter; | |
| 70 | + | - Both read and write methods cannot be **dynamic**: if they are **virtual** they cannot be **overload**. | |
| 71 | + | ||
| 72 | + | **default** sets a starting value for the property: it's optional and can be omitted. Another option is **stored false**: | |
| 73 | + | ||
| 74 | + | ``` pascal | |
| 75 | + | property SomeVar: Integer read GetSomeVar write SetSomeVar stored false; | |
| 76 | + | ``` | |
| 77 | + | ||
| 78 | + | This way the write method will be called, but the value will NOT be saved in the variable. | |
| 79 | + | ||
| 80 | + | ## self | |
| 81 | + | ||
| 82 | + | The **[[Self|self]]** argument is passed by default to every (non static) method: it's an alias to the specific class instance which the method belongs to. This way every class can identify itself and have access to its members without ambiguity. | |
| 83 | + | ||
| 84 | + | ## Method directives | |
| 85 | + | ||
| 86 | + | Class methods can be changed, overloaded, made static and more. A method can have more than one directive. Directives rule the whole polymorphism system in Object Pascal; they can change the call model, too. | |
| 87 | + | ||
| 88 | + | ### virtual, dynamic, override | |
| 89 | + | ||
| 90 | + | these three directives are mutually exclusive. **virtual** means that the method can be overwritten by the derived class. **dynamic** means the same thing, but the implementation differs: virtual members addresses are stored in a table, while dynamic members do not use tables and do not occupy RAM, but their resolution mechanism is slower. | |
| 91 | + | ||
| 92 | + | A derived class can implement its own version of a virtual method, but the base method is still available; if the new method is marked **override** it hides the base virtual method, which cannot be called anymore. This holds true in case of dynamic methods, too. However you cannot override a method if it's not virtual or dynamic. | |
| 93 | + | ||
| 94 | + | ### static | |
| 95 | + | ||
| 96 | + | A **static** method or variable is common to every instance of the class; one class instance can write a static variable member and every other instance will retain (and read, if accessed) the new value. For this reason, the compiler does NOT pass the 'self' parameter to static methods: it would be nonsense. Moreover, static methods cannot be virtual (but see class methods below). | |
| 97 | + | ||
| 98 | + | ### abstract | |
| 99 | + | ||
| 100 | + | A method declared **abstract** is declared, but not implemented in the base class. Derived classes will be forced to provide their own implementation. | |
| 101 | + | ||
| 102 | + | ### overload | |
| 103 | + | ||
| 104 | + | ``` pascal | |
| 105 | + | function MultiSum(a, b: integer): integer; overload; | |
| 106 | + | function MultiSum(a, b: AnsiString): AnsiString; overload; | |
| 107 | + | ``` | |
| 108 | + | ||
| 109 | + | With the **overload** directive you can declare many methods - functions or procedures - with the same name but different type and parameters. | |
| 110 | + | ||
| 111 | + | The class needs the constructor to be implemented in the implementation section of the unit: | |
| 112 | + | ||
| 113 | + | ``` pascal | |
| 114 | + | implementation | |
| 115 | + | ||
| 116 | + | constructor TMyClass.Create; | |
| 117 | + | begin | |
| 118 | + | inherited; | |
| 119 | + | SomeVar := 6; | |
| 120 | + | end; | |
| 121 | + | ||
| 122 | + | end. | |
| 123 | + | ``` | |
| 124 | + | ||
| 125 | + | The destructor and all procedures and functions of the class must also be implemented in the implementation section. | |
| 126 | + | ||
| 127 | + | Instances of the class can then be declared as variables: | |
| 128 | + | ||
| 129 | + | ``` pascal | |
| 130 | + | var | |
| 131 | + | classInstance: TMyClass; | |
| 132 | + | ``` | |
| 133 | + | ||
| 134 | + | Before using an instance, it must be created with: | |
| 135 | + | ||
| 136 | + | ``` pascal | |
| 137 | + | begin | |
| 138 | + | classInstance := TMyClass.Create; | |
| 139 | + | ... | |
| 140 | + | end; | |
| 141 | + | ``` | |
| 142 | + | ||
| 143 | + | ## Classes compared to other structured types | |
| 144 | + | ||
| 145 | + | ### See also | |
| 146 | + | ||
| 147 | + | [[extended class syntax]] Examples | |
| 148 | + | ||
| 149 | + | --- | |
| 150 | + | *From the Free Pascal wiki, page "Class", as it stood on 2022-02-12 after a change by Slawek, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
