IT SOLUTIONS
Your full service technology partner! 
-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

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

   ► KBProgrammingDelphi PrismOOP   Print This     
  From the February 2016 Issue of Prestwood eMag
 
Prism OOP:
Delphi Prism Abstraction (abstract, override)
 
Posted 16 years ago on 12/22/2008 and updated 1/28/2009
Prism Code Snippet:
 A flashcard from our Prism Flashcards Library
 A code snippet from our Prism Code Snippets Page

KB101753

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

Prism supports abstract class members and abstract classes using the abstract keyword.

An abstract class is a class with one or more abstract members and you cannot instantiate an abstract class. However, you can have additional implemented methods and properties.

An abstract member is either a method (method, procedure, or function), a property, or an event in an abstract class. You can add abstract members ONLY to abstract classes using the abstract keyword.

Alternatively, you can use the empty keyword in place of abstract if you wish to instantiate the abstract class. Then you override it in a descendant class with Override.

Syntax Example:
Cyborg = public abstract class(System.Object)
public
//You can put "virtual; abstract;"
  //but it's implied with just "abstract;"
  method Speak(pMessage: String); abstract; 
method Walk; virtual; abstract;
end;
 
Series600 = public class(Cyborg)
public
procedure Speak(pMessage: String); override;
procedure Walk; override;
end;

An Abstract Example

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

namespace CR_Abstraction;
 
interface
 
uses
System.Drawing,
System.Collections,
System.Collections.Generic,
System.Linq,
System.Windows.Forms,
System.ComponentModel;
 
type
///
/// Summary description for MainForm.
///

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;
 
  Cyborg = public abstract class(System.Object) 
protected
//Abstract methods for property.
method GetCyborgName: String; abstract;
method SetCyborgName(Value: String); abstract;

public
//Abstract property, regular property that uses abstract methods.
property CyborgName: String read GetCyborgName write SetCyborgName;
method Speak(pMessage: String); abstract;
method Walk; virtual; abstract;
end;
 
  Series600 = public class(Cyborg)
private
FCyborgName: String;
protected
method GetCyborgName: String; override;
method SetCyborgName(Value: String); override;

public
method Speak(pMessage: String); override;
method Walk; override;
end;
 
implementation
 
{$REGION Construction and Disposition}
constructor MainForm;
begin
//
// Required for Windows Form Designer support
//
InitializeComponent();
  //
// TODO: Add any constructor code after InitializeComponent call
//
end;
 
method MainForm.Dispose(disposing: Boolean);
begin
if disposing then begin
if assigned(components) then
components.Dispose();
 
      //
// TODO: Add custom disposition code here
//
end;
  inherited Dispose(disposing);
end;
{$ENDREGION}
 
method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);
begin
var MyKiller: Series600;
 
  MyKiller := New Series600;
 
  MyKiller.CyborgName := 'John';
MyKiller.Speak('I am ' + MyKiller.CyborgName + '.');
MyKiller.Speak('I am alive.');
end;
 
method Series600.Speak(pMessage: String);
begin
MessageBox.Show(pMessage);
end;
 
method Series600.Walk;
begin
//Impliment walk here.
end;
 
method Series600.GetCyborgName: String;
begin
Result := FCyborgName;
end;
 
method Series600.SetCyborgName(Value: String);
begin
FCyborgName := Value;
end;
 
end.

Summary

Abstraction is an important aspect of your software design and Prism implements a robust set of abstract features. 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

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 = P1142A1
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 #101753 Counter
15775
Since 12/22/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


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