Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal

How to instantiate Pascal classes

Muhammad Ashir

Pascal has two implementations for OOP, the object and the class. There are notable differences between the two.

An object is similar to a record in Pascal, except that it has methods. Methods can be procedures or functions. An object supports almost all the paradigms of OOP except for polymorphism. Moreover, an object is allocated on the stack.

In contrast, class supports polymorphism and is allocated on the heap. A variable of type class is actually a pointer to the class object that has been allocated on the heap.

Defining a class and instantiating it

Classes can only be defined in the outermost scope of a unit or a program, not in any function or procedure. They must be defined under the Type identifier.

type
   className = class [abstract | sealed] (ancestorClass)
      memberList
   end;

abstract and sealed are optional keywords. If a class is instantiated with the seal keyword, it means it cannot be extended through inheritance while abstract is used to declare a class as abstract, even if it does not contain any virtual or abstract methods. You can only use one of the two keywords while instantiating a class.

memberList refers to all the declarations of all the members of the class. This includes both variables and methods. A member’s visibility can be set using the keyword public, private, or protected. A public member is accessible from anywhere within the same module and outside the module as well. A private member is visible only within the same module as the class declaration, and a private member is only visible to classes that inherit from the base class.

RELATED TAGS

pascal

CONTRIBUTOR

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

View all Courses

Keep Exploring