Object-Oriented Programming in Delphi

August 4, 2024

Object-Oriented Programming in Delphi

Welcome to our lesson on Object-Oriented Programming (OOP) in Delphi! In this post, we will explore the core principles of OOP, including classes, objects, inheritance, and polymorphism. We will also provide coding examples to illustrate these concepts, helping you to understand how to implement OOP in your Delphi projects.

What is Object-Oriented Programming?

Object-Oriented Programming is a programming paradigm that organizes software design around data, or objects, rather than functions and logic. An object can be defined as a self-contained unit that contains both data and procedures (methods) that operate on the data. This approach promotes greater flexibility and maintainability in software development.

Classes and Objects

In Delphi, a class serves as a blueprint for creating objects. A class defines the properties (data) and methods (functions) that the objects created from it will have. To declare a class in Delphi, you use the following syntax:

type
  TMyClass = class
  private
    FName: string;
  public
    procedure SetName(AName: string);
    function GetName: string;
  end;

In this example, we define a class called TMyClass with a private field FName and two public methods: SetName and GetName.

To create an object from this class, you can do the following:

var
  MyObject: TMyClass;
begin
  MyObject := TMyClass.Create;
  MyObject.SetName('Delphi');
  WriteLn(MyObject.GetName);
  MyObject.Free;
end;

Inheritance

Inheritance allows a class to inherit properties and methods from another class. This promotes code reusability and establishes a relationship between classes. In Delphi, you can define a subclass using the following syntax:

type
  TAnimal = class
  public
    procedure Speak; virtual; abstract;
  end;

  TDog = class(TAnimal)
  public
    procedure Speak; override;
  end;

procedure TDog.Speak;
begin
  WriteLn('Woof!');
end;

In this example, TAnimal is a base class with an abstract method Speak. The class TDog inherits from TAnimal and provides its own implementation of the Speak method.

Polymorphism

Polymorphism enables you to use a single interface to represent different underlying forms (data types). In Delphi, polymorphism is typically achieved through method overriding. Continuing from our previous example, we can create an array of TAnimal objects and call their Speak method:

var
  Animals: array of TAnimal;
begin
  SetLength(Animals, 2);
  Animals[0] := TDog.Create;
  Animals[1] := TCat.Create; // Assuming TCat is another subclass of TAnimal

  for var Animal in Animals do
    Animal.Speak;

  for var Animal in Animals do
    Animal.Free;
end;

Conclusion

In this post, we explored the principles of Object-Oriented Programming in Delphi, focusing on classes, objects, inheritance, and polymorphism. These concepts are fundamental to writing organized, reusable, and maintainable code in Delphi. As you continue your journey in learning Delphi, practice implementing these OOP principles in your projects to enhance your coding skills.

Happy coding!