Chapter 3 – OOP Fundamentals
This chapter introduces the core Object-Oriented Programming concepts as implemented in Delphi XE2. It covers the foundational ideas that underpin Delphi's class-based architecture and prepares the reader for more advanced topics in later chapters.
Classes and Objects
Delphi's object model is built around the class type. A class defines a blueprint from which individual objects — instances — are created at runtime. Each object encapsulates both data (fields) and behaviour (methods), providing a clean separation of concerns and promoting code reuse.
Key topics covered include:
- Declaring classes with fields, methods, and properties
- Constructors and destructors — creating and destroying object instances
- Understanding the
Selfreference and object identity - Visibility specifiers:
private,protected,public, andpublished
Inheritance and Polymorphism Basics
Inheritance allows one class to derive from another, inheriting its members while adding or overriding functionality. Delphi supports single inheritance, where each class has exactly one immediate ancestor — ultimately tracing back to TObject, the root of the class hierarchy.
Polymorphism enables code written against a base class to operate on any derived class instance. Through virtual and dynamic methods, Delphi resolves the correct method implementation at runtime based on the actual object type.
- Declaring virtual methods with
virtualand overriding withoverride - The
inheritedkeyword for calling ancestor behaviour - Abstract methods and the
abstractdirective - Runtime type identification with the
isandasoperators