Introduction

Inheritance and polymorphism are fundamental concepts in object-oriented programming (OOP) that allow for the creation of flexible and reusable code. In Delphi/Object Pascal, these concepts enable developers to create complex systems with a clear and maintainable structure.

Key Concepts

  1. Inheritance: The mechanism by which one class (the child or derived class) inherits the properties and methods of another class (the parent or base class).
  2. Polymorphism: The ability of different classes to be treated as instances of the same class through a common interface, typically achieved through method overriding.

Inheritance

Basic Syntax

In Delphi/Object Pascal, inheritance is implemented using the class keyword. A derived class inherits from a base class using the class(TBaseClass) syntax.

type
  TBaseClass = class
  public
    procedure ShowMessage; virtual;
  end;

  TDerivedClass = class(TBaseClass)
  public
    procedure ShowMessage; override;
  end;

Explanation

  • TBaseClass: This is the base class with a method ShowMessage declared as virtual, indicating that it can be overridden in a derived class.
  • TDerivedClass: This is the derived class that inherits from TBaseClass and overrides the ShowMessage method.

Practical Example

program InheritanceExample;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TBaseClass = class
  public
    procedure ShowMessage; virtual;
  end;

  TDerivedClass = class(TBaseClass)
  public
    procedure ShowMessage; override;
  end;

procedure TBaseClass.ShowMessage;
begin
  Writeln('Message from Base Class');
end;

procedure TDerivedClass.ShowMessage;
begin
  Writeln('Message from Derived Class');
end;

var
  BaseObj: TBaseClass;
  DerivedObj: TDerivedClass;

begin
  BaseObj := TBaseClass.Create;
  DerivedObj := TDerivedClass.Create;

  BaseObj.ShowMessage; // Output: Message from Base Class
  DerivedObj.ShowMessage; // Output: Message from Derived Class

  BaseObj.Free;
  DerivedObj.Free;
end.

Explanation

  • BaseObj: An instance of TBaseClass that calls the ShowMessage method from the base class.
  • DerivedObj: An instance of TDerivedClass that calls the overridden ShowMessage method from the derived class.

Polymorphism

Basic Syntax

Polymorphism in Delphi/Object Pascal is achieved through method overriding and the use of base class references.

type
  TBaseClass = class
  public
    procedure ShowMessage; virtual;
  end;

  TDerivedClass = class(TBaseClass)
  public
    procedure ShowMessage; override;
  end;

Practical Example

program PolymorphismExample;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TBaseClass = class
  public
    procedure ShowMessage; virtual;
  end;

  TDerivedClass = class(TBaseClass)
  public
    procedure ShowMessage; override;
  end;

procedure TBaseClass.ShowMessage;
begin
  Writeln('Message from Base Class');
end;

procedure TDerivedClass.ShowMessage;
begin
  Writeln('Message from Derived Class');
end;

var
  Obj: TBaseClass;

begin
  Obj := TBaseClass.Create;
  Obj.ShowMessage; // Output: Message from Base Class

  Obj := TDerivedClass.Create;
  Obj.ShowMessage; // Output: Message from Derived Class

  Obj.Free;
end.

Explanation

  • Obj: A reference of type TBaseClass that can hold instances of both TBaseClass and TDerivedClass. This demonstrates polymorphism, where the method called depends on the actual object type at runtime.

Exercises

Exercise 1: Basic Inheritance

Create a base class TAnimal with a method Speak that outputs "Animal sound". Create a derived class TDog that overrides the Speak method to output "Bark".

Solution

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

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

procedure TAnimal.Speak;
begin
  Writeln('Animal sound');
end;

procedure TDog.Speak;
begin
  Writeln('Bark');
end;

var
  Animal: TAnimal;
  Dog: TDog;

begin
  Animal := TAnimal.Create;
  Dog := TDog.Create;

  Animal.Speak; // Output: Animal sound
  Dog.Speak; // Output: Bark

  Animal.Free;
  Dog.Free;
end.

Exercise 2: Polymorphism

Modify the previous exercise to use polymorphism. Create a base class reference and assign it to instances of both TAnimal and TDog.

Solution

var
  Animal: TAnimal;

begin
  Animal := TAnimal.Create;
  Animal.Speak; // Output: Animal sound

  Animal := TDog.Create;
  Animal.Speak; // Output: Bark

  Animal.Free;
end.

Common Mistakes and Tips

  • Forgetting to use virtual and override: Ensure that methods intended for overriding are declared with virtual in the base class and override in the derived class.
  • Memory Management: Always free objects to avoid memory leaks.
  • Type Casting: Be cautious with type casting when using polymorphism to avoid runtime errors.

Conclusion

In this section, we covered the basics of inheritance and polymorphism in Delphi/Object Pascal. These concepts are crucial for creating flexible and maintainable code. By understanding and applying these principles, you can design more robust and scalable applications. In the next section, we will delve into interfaces and abstract classes, further enhancing your OOP skills in Delphi/Object Pascal.

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