Solved

About object and heritage

Posted on 2002-04-18
7
143 Views
Last Modified: 2010-04-04

I have this :

type
TCar = class
  procedure Drive; virtual;
end;

TFord = class(TCar)
  procedure Drive; override;
end;

TExpedition = class(TFord)
  procedure Drive;
end;

Procedure TCar.Drive ;
Begin
  ShowMessage(‘From TCar’) ;
End ;

Procedure TFord.Drive ;
Begin
  ShowMessage(‘From TFord’) ;
End ;

Procedure TExpedition.Drive ;
Begin
  ShowMessage(‘From TExpedition’) ;
End ;

Prcedure test ;
Var  MyCar: TCar;
Begin
   MyCar := TExpedition.Create;
   MyCar.Drive;
End ;

If I execute the procedure Test , it is the Drive procedure from Tford that is executed.  Why ?  Why it is not the one from TExpedition ?  

Thanks

0
Comment
Question by:syloux
7 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
guessing, its because its not overridden,
and mycar is not from the texpedition-class

TExpedition(MyCar).Drive; //should output the texpedtion.drive

meikl ;-)

0
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility
beat me to it Meikl.... :-)

Meikl is correct on this. The TFord is overriding the TCar's drive procedure, but TExpedition has defined drive again, but without the override.

So, as Meikl said, it can be called with

TExpedition(MyCar).Drive

or if the intention was to override the base class:

TExpedition = class(TFord)
 procedure Drive; override;
end;

-------
Russell
0
 

Author Comment

by:syloux
Comment Utility
Sorry, I do not understand the mechanism :(((
Know any site where I can find explanation ?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Expert Comment

by:God_Ares
Comment Utility
Procedure test ;
Var  MyCar: Tcar; <<< why not  TExpedition ?
Begin
  MyCar := TExpedition.Create;
  MyCar.drive;
End ;
0
 
LVL 7

Accepted Solution

by:
God_Ares earned 50 total points
Comment Utility
well my vague explenation will be:

when you declare mycar as tcar (1)
and initiace it as texpedition, texpedition tford and tcar will be created tcar.drive will use tfords drive since it has been overridden in other words the adres that the cpu will jump to is tford's drive. well why should it jump to tcar's driv at all if you created it wit texpedition? look at (1) you declared it as an tcar, that's why. if you would decalre mycar as texpedition then texpedition's drive will be used.
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
yep
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
brummel . . .
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now