I.T. Discussion Community!
-Collapse +Expand
Prism
Search Prism Group:

Advanced
-Collapse +Expand Prism To/From
To/FromCODEGuides
-Collapse +Expand Prism Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Prism Store
PRESTWOODSTORE

Prestwood eMagazine

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

   ► KBProgrammingDelphi PrismOOP   Print This     
  From the March 2016 Issue of Prestwood eMag
 
Prism OOP:
Delphi Prism Overriding (virtual, override)
 
Posted 10 years ago on 3/7/2009
Prism Code Snippet:
 A flashcard from our Prism Flashcards Library
 A code snippet from our Prism Code Snippets Page

KB101946



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 Prism Overriding

Same as Delphi. In Prism, 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.

Use final to prevent further extending of a member and Sealed to prevent all members of a class from further extension.

Syntax Example:
Robot = class(System.Object)
public
method Speak; virtual;
end;
  
Cyborg = class(Robot)
public
method Speak; override;
end;

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;

 

More Info

Definition:  Method Overriding

Comments

0 Comments.
Share a thought or comment...
 
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 = P1226A1
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 #101946 Counter
13321
Since 3/7/2009
-
   Contact Us!
 
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

2,240 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech