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

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

   ► KBProgrammingDelphi for W...OOP   Print This     
  From the January 2016 Issue of Prestwood eMag
 
Delphi OOP:
Delphi Inheritance (=class(ParentClass))
 
Posted 11 years ago on 11/2/2008 and updated 1/28/2009
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page
 Tags: Delphi , Inheritance

KB101394



General Info: Inheritance

The concept of a class makes it possible to define subclasses that share some or all of the main class characteristics. This is called inheritance. Inheritance also allows you to reuse code more efficiently. In a class tree, inheritance is used to design classes vertically. (You can use Interfaces to design classes horizontally within a class tree.) With inheritance, you are defining an "is-a" relationship (i.e. a chow is-a dog). Analysts using UML call this generalization where you generalize specific classes into general parent classes.

Delphi Inheritance

In Delphi, you use the class keyword followed by the parent class in parens. If you leave out the parent class, your class inherits from TObject.

Syntax Example:

In the following example, a terminator T-600 is-an android. 

TAndroid = class
end;
 
T-600 = class(TAndroid)
end;

Now let's dive in...

Inheritance versus Interfaces

With inheritance, you design classes that have methods and attributes and then extend those methods and attributes in descendant classes adding functionality as the class hierarchy gets deeper and deeper. In a real sense, you design vertically top to bottom. Interfaces allow you to design horizontally througout your class tree. Interfaces define abstract methods and/or attributes in a psuedo abstract class that if used on a class must be implemented by the class.

Complete Example

More complete example...

//Declare class in Interface section.
TPerson = class(TObject)
private
  FName: String;
  FAge: Integer;
public
  constructor Create;
end;
//Implement class in Implementation section.
constructor TPerson.Create;
begin
  inherited;  //Call the parent Create method
  // Now set a default fruit name
  FName := 'unknown';
  FAge := 0;
end;
//Use class.
var
  Lisa: TPerson;
begin
  Lisa := TPerson.Create;
  //Defaults.
  ShowMessage(Lisa.FName + ' is ' + IntToStr(Lisa.FAge));
  //Use class.
  Lisa.FName := 'Lisa';
  Lisa.FAge := 34;
  ShowMessage(Lisa.FName + ' is ' + IntToStr(Lisa.FAge));
end;

Step by Step Example

The following is a step-by-step tutorial on creating your first Delphi class and does include an inheritance example. From this simple tutorial, you can then experiment with the various class inheritance features.

 

More Info

Definition:  Inheritance

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


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Intermediate

1 Intermediate Level Question

Question #1: True or False?

A single class can inherit from a parent class and implement one "or more" interfaces.

Answer:
  • True
  • False

  •  KB Article #101394 Counter
    26173
    Since 11/2/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]