Every object-oriented programming (OOP) language involves concepts of objects, classes, inheritance, polymorphism, etc.
Pascal is a strongly typed object-oriented language and uses classes to perform various functionalities. In Pascal, classes are allocated on the heap.
A class defines the structure of a program. They are basically the blueprint of your program and includes characteristics such as:
A class is defined similar to the way an object is specified i.e., using the type
keyword.
We implement classes in a program to apply proper object-oriented behavior, as objects in Pascal do not take part in
The following code snippet shows the basics of how to declare a class
and its attributes such as constructors,
procedures,
function,
etc.
type class-name = class
private
field1 : integer;
field2 : string;
field2 : boolean;
...
public
constructor create();
procedure proc1;
function func1(): function-type;
function func2(param1, param2, integer): integer; overload;
end;
var classvar : class-name;
private
and public
are the type or scope modifiers of a class that increases or decreases code visibility to the user.
create
is a pre-defined constructor in Pascal that helps us create constructors for a class. There can be more than one constructor in a class, while only one destructor is allowed for each class.
private
and public
keywordsAs described above, these keywords are scope modifiers of a class type. The main difference between them is:
We cannot read or write from another module if it is declared as a private
field or property.
A private member is invisible outside of the unit or program where its class is declared.
If a member is declared public
, it is visible wherever its class can be referenced.
We can read or write from another module if it is declared as a public
field or property.
Pascal supports the concept of abstraction, which allows hiding the implementation details and showing only functionality to the user. Abstract classes are only inherited from other classes and not explicitly instantiated.
We can create an abstract class in Pascal the same way other classes are defined. However, we add the abstract
keyword before the class name to make it abstract.
type class-name = abstract class (Root)
procedure proc1; abstract;
function func1(): function-type;
...
end;
Note: A class in Pascal can be declared abstract even if it does not contain any
. abstract virtual methods A method is a virtual or dynamic method with no implementation in the class where it is declared. We should write its implementation to a descending class
Pascal has introduced a new concept, i.e., Sealed Classes.
sealed
is a keyword in Pascal that makes a class unable to support inheritance. By making a class, sealed
means that it cannot extend through inheritance.
type class-name = class sealed
procedure proc1;
function func1(): function-type;
other memberList;
...
end;
Free Resources