IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

October Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...OOP   Print This     
  From the November 2015 Issue of Prestwood eMag
 
Delphi OOP:
Delphi Overriding (virtual, override)
 
Posted 15 years ago on 3/7/2009 and updated 3/30/2010
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page
 Tags: Delphi , Overriding

KB101945

General Info: Method Overriding

Where you define or implement a virtual method in a parent class and then replace it in a descendant class.

When you decide to declare a method as virtual, you are giving permission to derived classes to extend and override the method with their own implementation. You can have the extended method call the parent method's code too.

In most OO languages you can also choose to hide a parent method. When you introduce a new implementation of the same named method with the same signature without overriding, you are hiding the parent method.

Delphi Overriding

In Delphi, you specify a virtual method with the virtual keyword in a parent class and extend (or replace) it in a descendant class using the override keyword. Call Inherited in the descendant method to execute the code in the parent method.

Syntax Example:
TRobot = class(TObject)
public
procedure Speak; virtual;
end;
  
TCyborg = class(TRobot)
procedure Speak; Override;
end;

Working Delphi 2009 Override Example

The following code assumes a Windows application with a single form�with a button. It�demonstrates using virtual and override to override a parent method in a descendant class.

unit Unit1;
  
interface  
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
  
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;
  
  TRobot = class(TObject)
public
procedure Speak; virtual;
end;
  
  TCyborg = class(TRobot)
procedure Speak; override;
end;
  
var
Form1: TForm1;
  
implementation
{$R *.dfm}
  
procedure TRobot.Speak;
begin
  ShowMessage('Robot says hi');
end;
procedure TCyborg.Speak;
begin
  ShowMessage('Cyborg says hi');
end;
  
procedure TForm1.Button1Click(Sender: TObject);
var
Robot: TRobot;
Cyborg: TCyborg;
begin
Robot := TRobot.Create;
Cyborg := TCyborg.Create;
  
  Robot.Speak;
Cyborg.Speak;
  
  FreeAndNil(Robot);
FreeAndNil(Cyborg);
end;
end.

Hiding a Method with 'reintroduce'

Use the reintroduce keyword to introduce a new implementation of a parent method (this hides the parent method). You can hide a method without using�reintroduce but you will get a compiler warning. Using�reintroduce will suppress the warning.

The�reintroduce and override modifiers have different meanings. The�reintroduce modifier creates a new member with the same name, signature, and visibility and hides the original member. The override modifier extends the implementation for an inherited member and allows you to implement inheritance-based polymorphism.

Avoid Introducing New Members: Sometimes there are clear reasons to introduce a new method with the same name, signature, and visibility of a parent method. In those clear cases, introducing a new member is a powerful feature. However, if you do not have a clear reason, then avoid introducing a new version of a method by naming the new method something unique and appropriate.

//interface section:
TRobot = class(TObject)
public
procedure Speak;
end;
  
TCyborg = class(TRobot)
procedure Speak; reintroduce;
end;
  
//implementation section:
procedure TRobot.Speak;
begin
ShowMessage('Robot says hi');
end;
  
procedure TCyborg.Speak;
begin
  ShowMessage('Cyborg says hi');
end;

Invoking the Parent Method with 'inherited'

A common task In OO is to extend a method by first executing the parent method code and then adding code. Use the Inherited keyword to invoke the parent class method.

method Robot.Speak;
begin
MessageBox.Show("Robot says hi");
end;
  
method Cyborg.Speak;
begin
inherited;
MessageBox.Show("Cyborg says hi");
end;

More Info

Definition:  Method Overriding

Comments

1 Comments.
Share a thought or comment...
Comment 1 of 2

Thanks for the walk through introduce and overload directives.

Cheers.

---
Robert
Posted 13 years ago

Comment 2 of 2

THanks very mucH for tHe examples

---
DB
Posted 10 years ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P146A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101945 Counter
28198
Since 3/7/2009
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]