Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications and computer programs. It utilizes several key concepts such as classes, objects, inheritance, polymorphism, encapsulation, and abstraction. In this section, we will introduce these concepts and explain how they are implemented in Delphi/Object Pascal.

Key Concepts of OOP

  1. Classes and Objects

  • Class: A blueprint for creating objects. It defines a datatype by bundling data and methods that work on the data into one single unit.
  • Object: An instance of a class. When a class is defined, no memory is allocated until an object of that class is created.

  1. Inheritance

  • Inheritance: A mechanism where a new class inherits properties and behavior (methods) from an existing class. The new class is called a derived class or subclass, and the existing class is called a base class or superclass.

  1. Polymorphism

  • Polymorphism: The ability of different classes to be treated as instances of the same class through inheritance. It allows methods to do different things based on the object it is acting upon.

  1. Encapsulation

  • Encapsulation: The bundling of data and methods that operate on the data within one unit, e.g., a class. It restricts direct access to some of the object's components, which can prevent the accidental modification of data.

  1. Abstraction

  • Abstraction: The concept of hiding the complex implementation details and showing only the necessary features of an object. It helps in reducing programming complexity and effort.

Implementing OOP in Delphi/Object Pascal

Defining a Class

In Delphi, a class is defined using the class keyword. Here is a simple example:

type
  TPerson = class
  private
    FName: string;
    FAge: Integer;
  public
    constructor Create(Name: string; Age: Integer);
    procedure DisplayInfo;
  end;
  • Private Section: Contains data members that are not accessible outside the class.
  • Public Section: Contains methods and data members that are accessible from outside the class.

Creating an Object

To create an object of the class TPerson, you use the Create method:

var
  Person: TPerson;
begin
  Person := TPerson.Create('John Doe', 30);
  Person.DisplayInfo;
  Person.Free; // Free the memory allocated for the object
end;

Constructor and Methods

The constructor is a special method used to initialize objects. Here is how you define and implement the constructor and a method in Delphi:

constructor TPerson.Create(Name: string; Age: Integer);
begin
  FName := Name;
  FAge := Age;
end;

procedure TPerson.DisplayInfo;
begin
  WriteLn('Name: ', FName);
  WriteLn('Age: ', FAge);
end;

Inheritance

To demonstrate inheritance, let's create a subclass TStudent that inherits from TPerson:

type
  TStudent = class(TPerson)
  private
    FStudentID: string;
  public
    constructor Create(Name: string; Age: Integer; StudentID: string);
    procedure DisplayInfo; override;
  end;
  • Override: The override keyword is used to indicate that a method in the subclass is intended to replace a method in the base class.
constructor TStudent.Create(Name: string; Age: Integer; StudentID: string);
begin
  inherited Create(Name, Age); // Call the base class constructor
  FStudentID := StudentID;
end;

procedure TStudent.DisplayInfo;
begin
  inherited DisplayInfo; // Call the base class method
  WriteLn('Student ID: ', FStudentID);
end;

Polymorphism

Polymorphism allows you to call methods of derived classes through a base class reference. Here is an example:

var
  Person: TPerson;
  Student: TStudent;
begin
  Person := TPerson.Create('John Doe', 30);
  Student := TStudent.Create('Jane Doe', 22, 'S12345');

  Person.DisplayInfo;
  Student.DisplayInfo;

  Person := Student; // Polymorphism in action
  Person.DisplayInfo; // Calls TStudent's DisplayInfo method

  Person.Free;
  Student.Free;
end;

Practical Exercise

Exercise 1: Create a Class and Object

  1. Define a class TCar with private fields FMake, FModel, and FYear.
  2. Create a constructor to initialize these fields.
  3. Implement a method DisplayInfo to print the car's details.
  4. Create an object of TCar and call DisplayInfo.

Solution

type
  TCar = class
  private
    FMake: string;
    FModel: string;
    FYear: Integer;
  public
    constructor Create(Make, Model: string; Year: Integer);
    procedure DisplayInfo;
  end;

constructor TCar.Create(Make, Model: string; Year: Integer);
begin
  FMake := Make;
  FModel := Model;
  FYear := Year;
end;

procedure TCar.DisplayInfo;
begin
  WriteLn('Make: ', FMake);
  WriteLn('Model: ', FModel);
  WriteLn('Year: ', FYear);
end;

var
  Car: TCar;
begin
  Car := TCar.Create('Toyota', 'Corolla', 2020);
  Car.DisplayInfo;
  Car.Free;
end;

Exercise 2: Implement Inheritance

  1. Define a subclass TElectricCar that inherits from TCar.
  2. Add a private field FBatteryLife to TElectricCar.
  3. Create a constructor to initialize all fields, including those from TCar.
  4. Override the DisplayInfo method to include battery life information.

Solution

type
  TElectricCar = class(TCar)
  private
    FBatteryLife: Integer;
  public
    constructor Create(Make, Model: string; Year, BatteryLife: Integer);
    procedure DisplayInfo; override;
  end;

constructor TElectricCar.Create(Make, Model: string; Year, BatteryLife: Integer);
begin
  inherited Create(Make, Model, Year);
  FBatteryLife := BatteryLife;
end;

procedure TElectricCar.DisplayInfo;
begin
  inherited DisplayInfo;
  WriteLn('Battery Life: ', FBatteryLife, ' hours');
end;

var
  ElectricCar: TElectricCar;
begin
  ElectricCar := TElectricCar.Create('Tesla', 'Model S', 2021, 24);
  ElectricCar.DisplayInfo;
  ElectricCar.Free;
end;

Conclusion

In this section, we introduced the fundamental concepts of Object-Oriented Programming (OOP) and demonstrated how to implement them in Delphi/Object Pascal. We covered classes, objects, inheritance, polymorphism, encapsulation, and abstraction. By understanding and applying these concepts, you can create more modular, reusable, and maintainable code. In the next section, we will delve deeper into classes and objects, exploring more advanced features and techniques.

Delphi/Object Pascal Programming Course

Module 1: Introduction to Delphi/Object Pascal

Module 2: Control Structures and Procedures

Module 3: Working with Data

Module 4: Object-Oriented Programming

Module 5: Advanced Delphi Features

Module 6: GUI Development with VCL and FMX

Module 7: Web and Mobile Development

Module 8: Best Practices and Design Patterns

Module 9: Final Project

© Copyright 2024. All rights reserved