Override Details
- You cannot override a regular non-virtual method, nor a static method.
- The first version of the parent method must be virtual or abstract.
- You can override any parent method marked virtual, abstract, or override (already overridden).
- The methods must have the same signature.
- The methods must have the same visibility (the same access level).
- Use the Inherited keyword to refer to cal the parent class method.
Working Prism 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.
namespace CR_Override;
interface
uses
System.Drawing,
System.Collections,
System.Collections.Generic,
System.Linq,
System.Windows.Forms,
System.ComponentModel;
type
/// <summary>
/// Summary description for MainForm.
/// </summary>
MainForm = partial class(System.Windows.Forms.Form)
private
method button1_Click(sender: System.Object; e: System.EventArgs);
protected
method Dispose(disposing: Boolean); override;
public
constructor;
end;
Robot = class(System.Object)
public
method Speak; virtual;
end;
Cyborg = class(Robot)
public
method Speak; override;
end;
implementation
{$REGION Construction and Disposition}
constructor MainForm;
begin
InitializeComponent();
end;
method MainForm.Dispose(disposing: Boolean);
begin
if disposing then begin
if assigned(components) then
components.Dispose();
end;
inherited Dispose(disposing);
end;
{$ENDREGION}
method Robot.Speak;
begin
MessageBox.Show("Robot says hi");
end;
method Cyborg.Speak;
begin
MessageBox.Show("Cyborg says hi");
end;
method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);
begin
var MyRobot: Robot;
var MyCyborg: Cyborg;
MyRobot := new Robot;
MyCyborg := new Cyborg;
MyRobot.Speak;
MyCyborg.Speak;
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:
Robot = class(System.Object)
public
method Speak;
end;
Cyborg = class(Robot)
public
method Speak; reintroduce;
end;
//implementation section:
method Robot.Speak;
begin
MessageBox.Show("Robot says hi");
end;
method Cyborg.Speak;
begin
MessageBox.Show("Cyborg says hi");
end;
Calling the Base Class Version
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;