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

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

   ► KBProgrammingDelphi for W...OOP   Print This     
  From the February 2016 Issue of Prestwood eMag
 
Delphi OOP:
Delphi Abstraction (abstract, override)
 
Posted 11 years ago on 10/23/2008 and updated 1/30/2011
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page
 Tags: Delphi , Abstraction

KB101346



General Info: Abstract Class / Abstract Member

An abstract class member is a member that is specified in a class but not implemented. Classes that inherit from the class will have to implement the abstract member. Abstract members are a technique for ensuring a common interface with descendant classes. An abstract class is a class you cannot instantiate. A pure abstract class is a class with only abstract members.

Languages Focus: Abstraction

Abstraction is supported at various levels with each language. A language could enforce abstraction at the class level (either enforcing a no-instantiation rule or a only abstract members rule), and with class members (member methods and/or properties).

Delphi Abstraction

Delphi for Win32 supports abstract class members using the abstract keyword. You can even instantiate instances of a class that contains abstract members. Then you override each abstract member in a descendant class with Override.

Delphi does not support setting an entire class as abstract. You can create an abstract class (a class with one or more abstract methods), but there is no way to tell the compiler to not allow the instantiation of the abstract class.

Delphi does not support abstract member properties directly. To implement an abstract properity, make use of abstract methods. That is, you can read a GetPropertyX abstract function and write to a SetPropertyX abstract procedure. In effect, creating  an abstract property.

Syntax Example:
TCyborg = class(TObject)
public
  procedure Speak(pMessage: String); virtual; abstract;
  procedure Walk; virtual; abstract;
end;
 
TSeries600 = class(TCyborg)
public
  procedure Speak(pMessage: String); override;
  procedure Walk; override;
end;

Now let's dig deeper...

Virtual Abstract versus Dynamic Abstract

For abstract methods, you must specify either regular virtual with the virtual keyword or dynamic virtual with the dynamic keyword. In Delphi for Win32, virtual methods are optimized for speed and dynamic methods are optimized for size. The Delphi help indicates to use virtual for most situations.

It is true that the compiler could make virtual the default and therefore optional but requiring one or the other is consistent with Object Pascal's strong typing. However, with Delphi Prism, the use of the virtual keyword with abstract methods is optional.

An Abstract Example with an Abstract Property

The following demonstrates the abstract method above and also contains an abstract property (or rather, a regular property that makes use of an abstract method). The following form unit assumes a form with a button.

unit AbstractionUnit;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;
 
  TCyborg = class(TObject)
  private
    FCyborgName: String;
  protected
    procedure SetCyborgName(const Value: String); virtual; abstract;
  public
    property CyborgName: String read FCyborgName write SetCyborgName;
    procedure Speak(pMessage: String); virtual; abstract;
    procedure Walk; virtual; abstract;
  end;
 
  TSeries600 = class(TCyborg)
  protected
    procedure SetCyborgName(const Value: String); override;
  public
    procedure Speak(pMessage: String); override;
    procedure Walk; override;
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TSeries600.Speak(pMessage: String);
begin
  ShowMessage(pMessage);
end;
 
procedure TSeries600.SetCyborgName(const Value: String);
begin
  FCyborgName := Value;
end;
 
procedure TSeries600.Walk;
begin
  //Implement here.
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  MyKiller: TSeries600;
begin
  MyKiller := TSeries600.Create;
  MyKiller.CyborgName := 'John';
  MyKiller.Speak('I am ' + MyKiller.CyborgName + '.');
  MyKiller.Speak('I''ll be back.');
end;
 
end.

Summary

Abstraction is an important aspect of your software design. To learn more about how abstract classes are similar concepts to interfaces, how they relate to Plato's Forms theory, and more, read our Abstract Members / Class definition article next.

More Info

Definition:  Abstract Class / Abstract Member

Comments

1 Comments.
Share a thought or comment...
Comment 1 of 3
Syntax Example:
TCyborg = class(TObject)
public
  procedure Speak(pMessage: String); virtual; abstract;
  procedure Walk; abstract; //WRONG SYNTAX, YOU MUST USE VIRTUAL AS YOU HAVE
	//WRITTEN IN EXAMPLE, SO YOU CAN'T OVERRIDE THIS METHOD, AS YOU 
         //SHOWN IN SUB-CLASS
end;
 
TSeries600 = class(TCyborg)
public
  procedure Speak(pMessage: String); override;

  procedure Walk; override;
end;
---
Vishal
Posted 9 years ago

Comment 2 of 3

Hi Vishal,

You are indeed correct as the example and text shows. My bad. I did update the first example with virtual; abstract; so it is now correct.

Posted 8 years ago

Comment 3 of 3

Hi Mike,

Can you please explain how to use DYNAMIC abstract methods.

---
raj
Posted 39 months 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 = P1212A1
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 #101346 Counter
23748
Since 10/23/2008
-
   Contact Us!
 
Have a question? Need our services? Contact us now.
--Mike Prestwood

Call: 916-726-5675

email: info@prestwood.com


Go ahead!   Use Us! Call: 916-726-5675 


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