How to Access the Base (Super) Class In Delphi?

11 minutes read

To access the base (super) class in Delphi, you can use the inherited keyword. The inherited keyword is used within a derived class to call the implementation of a method or property in its parent class.


When you inherit a class in Delphi, you can override its methods or properties in the derived class. However, sometimes you may want to call the original implementation of the overridden method from the base (super) class. This is where the inherited keyword comes into play.


To access the base (super) class, follow these steps:

  1. In your derived class, declare a new method or property that overrides the method or property of the base class.
  2. Inside the derived method or property, use the inherited keyword followed by the name of the method or property you want to access from the base class.
  3. You can then add your additional code or modifications to the base class implementation.


Here's an example to illustrate the usage of inherited:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
type
  TMyBaseClass = class
    procedure MyMethod; virtual;
  end;

  TMyDerivedClass = class(TMyBaseClass)
    procedure MyMethod; override;
  end;

procedure TMyBaseClass.MyMethod;
begin
  // Base class implementation
end;

procedure TMyDerivedClass.MyMethod;
begin
  inherited MyMethod; // Access the base class implementation

  // Additional code or modifications specific to the derived class
end;


In the example above, TMyBaseClass is the base class, and TMyDerivedClass is the derived class. The MyMethod is overridden in the derived class, but using inherited MyMethod, you can call the original implementation of the MyMethod from the base class before adding any modifications specific to the derived class.


By using the inherited keyword, you can conveniently access and utilize the functionality provided by the base (super) class in Delphi.

Best Delphi Books to Read in 2024

1
Delphi GUI Programming with FireMonkey: Unleash the full potential of the FMX framework to build exciting cross-platform apps with Embarcadero Delphi

Rating is 5 out of 5

Delphi GUI Programming with FireMonkey: Unleash the full potential of the FMX framework to build exciting cross-platform apps with Embarcadero Delphi

2
Mastering Delphi Programming: A Complete Reference Guide: Learn all about building fast, scalable, and high performing applications with Delphi

Rating is 4.9 out of 5

Mastering Delphi Programming: A Complete Reference Guide: Learn all about building fast, scalable, and high performing applications with Delphi

3
Delphi Cookbook: Recipes to master Delphi for IoT integrations, cross-platform, mobile and server-side development, 3rd Edition

Rating is 4.8 out of 5

Delphi Cookbook: Recipes to master Delphi for IoT integrations, cross-platform, mobile and server-side development, 3rd Edition

4
Delphi Programming for Dummies

Rating is 4.7 out of 5

Delphi Programming for Dummies

5
Delphi Cookbook - Second Edition

Rating is 4.6 out of 5

Delphi Cookbook - Second Edition

6
Mastering Pascal and Delphi Programming (Palgrave Master Series (Computing), 1)

Rating is 4.5 out of 5

Mastering Pascal and Delphi Programming (Palgrave Master Series (Computing), 1)

7
Delphi Programming Projects: Build a range of exciting projects by exploring cross-platform development and microservices

Rating is 4.4 out of 5

Delphi Programming Projects: Build a range of exciting projects by exploring cross-platform development and microservices


What is the role of the inherited keyword in Delphi?

In Delphi, the inherited keyword is used to call the overridden method from the base class within a descendant class. It is typically used inside an overridden method to ensure that the parent class's behavior is preserved while adding or modifying functionality.


When a method is overridden in a descendant class, the inherited keyword allows the descendant class to access and call the original implementation of the method defined in the base class. This provides a way to extend or modify the behavior of the method without completely rewriting it.


By calling inherited within an overridden method, the descendant class can add additional instructions before or after the original implementation, or selectively override only certain aspects of the method. This helps to keep the code organized, promotes code reuse, and ensures that the base class's behavior is not lost.


Here's an example to illustrate the use of inherited in Delphi:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
unit MyBaseClass;

interface

type
  TBaseClass = class
    procedure DoSomething; virtual;
  end;

implementation

procedure TBaseClass.DoSomething;
begin
  // Base class's implementation
end;

end.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
unit MyDescendantClass;

interface

uses
  MyBaseClass;

type
  TDescendantClass = class(TBaseClass)
    procedure DoSomething; override;
  end;

implementation

procedure TDescendantClass.DoSomething;
begin
  // Additional instructions

  inherited; // Call the overridden method in the base class

  // Additional instructions
end;

end.


In this example, the TBaseClass defines a method called DoSomething. The TDescendantClass inherits from TBaseClass and overrides the DoSomething method.


Inside TDescendantClass.DoSomething, the inherited keyword is used to call the original implementation of DoSomething from TBaseClass. This ensures that the base class's behavior is preserved while allowing the descendant class to add or modify functionality.


By including inherited, the descendant class can extend or customize the behavior of the base class's method without losing any of its original functionality.


How to call a method from the base class in Delphi?

To call a method from the base class in Delphi, you can use the inherited keyword.


Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
type
  TBaseClass = class
    procedure PrintMessage;
  end;
  
  TDerivedClass = class(TBaseClass)
    procedure PrintMessage;
  end;

procedure TBaseClass.PrintMessage;
begin
  // implementation of the base class method
  WriteLn('This is the base class message');
end;

procedure TDerivedClass.PrintMessage;
begin
  // calling the base class method using the inherited keyword
  inherited;

  // additional implementation for the derived class
  WriteLn('This is the derived class message');
end;


In this example, the PrintMessage method is defined in both the base class TBaseClass and the derived class TDerivedClass. By calling inherited; in the derived class method, you are invoking the implementation of the base class method before adding any additional functionality specific to the derived class.


When you create an instance of TDerivedClass and call its PrintMessage method, it will execute the base class method first and then execute the derived class method, producing the following output:

1
2
This is the base class message
This is the derived class message



What is the purpose of accessing the base class in Delphi?

In Delphi, accessing the base class allows you to inherit and reuse functionality from a parent class. The purpose of accessing the base class is to leverage the existing methods, properties, and events defined in the parent class while extending or modifying them as per your requirements in the derived class. This concept is known as inheritance and is a fundamental principle of object-oriented programming.


By accessing the base class, you can access all the public and protected members of the parent class, including variables, methods, and properties. This helps in code reusability, promotes efficient and structured programming, and allows you to create hierarchical relationships among classes. With base class access, you can override inherited methods or add new methods, properties, and events in the derived class to customize its behavior.


Overall, accessing the base class allows you to build upon an existing class, reducing code duplication, and creating more flexible and maintainable applications.


What is the keyword to access the base class in Delphi?

The keyword to access the base class in Delphi is "inherited".


How to access the inherited method implementation in Delphi?

To access the inherited method implementation in Delphi, you need to use the inherited keyword followed by the method name. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
type
  TMyClass = class
    procedure MyMethod(); virtual;
  end;

  TMySubClass = class(TMyClass)
    procedure MyMethod(); override;
  end;

procedure TMyClass.MyMethod();
begin
  // Your implementation here
end;

procedure TMySubClass.MyMethod();
begin
  inherited; // Access the inherited method implementation
  // Additional code here
end;


In the above example, TMyClass defines a virtual method called MyMethod. In TMySubClass, which inherits from TMyClass, you can access the inherited implementation of MyMethod using the inherited keyword. This allows you to execute the code defined in the parent class and add additional code specific to the subclass.


How to reference the base class in Delphi?

To reference the base class in Delphi, you need to use the inherited keyword.


The inherited keyword allows you to access the members of the base class within the context of a derived class. Here's how you can use it:

  1. Use the inherited keyword followed by the name of the member (method or property) you want to access from the base class. For example, to call a method named "MyMethod" from the base class, you would use inherited MyMethod;.
  2. If the base class method has parameters, pass the required arguments in the parentheses. For example, if the base class method "MyMethod" takes a single integer parameter, you would use inherited MyMethod(42);.
  3. You can also assign the return value of the base class method to a variable by using the inherited keyword in an assignment statement. For example, if the base class method "MyMethod" returns an integer, you can assign it to a variable like this: MyVar := inherited MyMethod;.


Here's an example that demonstrates how to reference a base class method within a derived class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
unit MyBaseClassUnit;

interface

type
  TMyBaseClass = class
  public
    procedure MyMethod; virtual;
  end;

implementation

procedure TMyBaseClass.MyMethod;
begin
  // Base class implementation
end;

end.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
unit MyDerivedClassUnit;

interface

uses
  MyBaseClassUnit;

type
  TMyDerivedClass = class(TMyBaseClass)
  public
    procedure MyMethod; override;
  end;
  
implementation

procedure TMyDerivedClass.MyMethod;
begin
  inherited MyMethod; // Calling the base class implementation of MyMethod
  // Derived class implementation
end;

end.


In this example, the derived class TMyDerivedClass overrides the MyMethod of the base class TMyBaseClass but still wants to execute the base class implementation. This is achieved by using inherited MyMethod; in the derived class MyMethod implementation.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

To install the Chromium package in Delphi, you can follow these steps:Download the appropriate version of the Chromium package for Delphi from the official website or from a trusted source.Extract the downloaded package to a folder on your computer.Open your D...
To get the same MD5 hash with Delphi and PHP, you can follow the steps outlined below:Ensure that both your Delphi and PHP implementations use the same input data when generating the MD5 hash. This means that the string or file being hashed should be the same ...
To read an XML file in Delphi, you can use the built-in XML handling capabilities of the Delphi programming language. Here's how you can do it:First, you need to include the Xml.XMLIntf and Xml.XMLDoc units in your Delphi code. These units provide the nece...