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

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

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

   ► KBProgrammingDelphi for W...OOP   Print This     
  From the August 2015 Issue of Prestwood eMag
 
Delphi OOP:
Delphi Base Class (TObject)
 
Posted 11 years ago on 10/24/2008 and updated 2/9/2009
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page
 Tags: Delphi , Base Class

KB101411



Languages Focus: Base Class

When you create a class, it is either a base class or inherits from another class. Some languages require all classes to inherit from a common base class and some do not.

Delphi Base Class

In Delphi programming language (Object Pascal), all classes ultimately inherit from the base class TObject.

Syntax Example:
//Specify both namespace and class:
TCyborg = class(System.TObject)
end;
  
//Use shortcut alias:
TCyborg = class(TObject)
end;
  
//None, default is System.TObject
TCyborg = class
end;

Fun With TObject

TObject has some basic functionality available for all Delphi classes. Since TObject is the base class for Delphi, you should become familiar with it's features.

To see TObject in action, alter the click event of a button as follows:

var
  MyObject: TObject;

begin
  MyObject := TObject.Create;       //Create object instance.
  ShowMessage(MyObject.ClassName);  //Return the class name.
  ShowMessage(UnitName);            //UnitName is a class function
                                    //defined in TObject.
  MyObject.Free;                   
//Free object instance.
end;

Next, view the source code for TObject to see what routines are implemented. The easiest way to find TObject's declaration is to right click on TObject in your source code and select Find Declaration. Most of these routines are documented in the help so you can place the cursor on any procedure or function name and press F1.

Common Inheritance Choices

The following is a short list of some more common VCL classes you can inherit from.

Class Use For Features / Notes
TObject demos create, destroy
TPersistent streaming objects

Save object

TObject >> TPersistent

TComponent non-visual classes

Ability to appear on the Component / Tool palette and to be manipulated in the Form Designer.

TObject >> TPersistent >> TComponent

TControl visual classes

Visual elements like OnClick, OnDblClick, Top, Left, etc.

TObject >> TPersistent >> TComponent >> TControl

IInterface Interfaces Base interface class.
IUnknown Win 32 Specific Interfaces For Win32 specific interfaces.

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


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: Multiple Choice

Given the following three code snippets:

1. TPerson = class(TObject)
end;
 
2. TPerson = class
end;
  
3. TPerson = class(System.TObject)
end;

Which of the following statement is most accurate?

Answer:
1. 

1 and 3 are equivalent, 2 will not even compile.

2. 

All three are equivalent.

3. 

2 and 3 are equivalent, 1 will not even compile.

4. 

1 and 2 are equivalent, 3 will not even compile.


 KB Article #101411 Counter
22954
Since 10/24/2008
-
   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,216 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