Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal

What are abstract and sealed classes in Pascal?

Muhammad Ashir

Object-oriented behavior in Pascal is implemented in two different ways. You can either use objects or classes to implement object-oriented programming. Both objects and classes support most of the paradigms of OOP; however, there are some notable differences between the two.

Property

Objects

Classes

Encapsulation

yes

yes

Inheritance

yes

yes

Class constructor and destructor

yes

yes

Polymorphism (virtual methods)

yes

no

Memory Allocation

Stack

Heap

A class can be declared as either sealed or abstract in Pascal.

Abstract classes

As shown in the table above, only classes support polymorphism. Polymorphism is a feature of object-oriented programming that allows different classes to have the same interface for representing different data and operations.

Abstract classes are only inherited from other classes and cannot be explicitly instantiated.

Abstract classes are created the same way as regular classes. However, using the abstract keyword before the class identifier makes it an abstract class. A class in Pascal can be declared abstract even if it does not contain any virtual methods.

Type
    className = abstract class (ParentClass)
        var1: type, var2: type, ...
        procedure method_1; abstract;
        function method_2(); return-type;
end;

Sealed classes

The keyword sealed is used when you don’t want to support inheritance in your class. It means that the class cannot be extended through derived classes or use them as a base for further child classes.

Type
    className = class sealed
        var1: type, var2: type, ...
        procedure method_1; abstract;
        function method_2(); return-type;
end;

RELATED TAGS

pascal

CONTRIBUTOR

Muhammad Ashir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring