Solved

About object and heritage

Posted on 2002-04-18
7
144 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
ID: 6950571
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
ID: 6950601
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
ID: 6950879
Sorry, I do not understand the mechanism :(((
Know any site where I can find explanation ?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:God_Ares
ID: 6950881
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
ID: 6950910
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
ID: 6950918
yep
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6952995
brummel . . .
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
System restore point 4 91
delphi exception 7 63
Best Firemonkey component pack 1 87
Strange behavior when a form is closed 6 51
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

11 Experts available now in Live!

Get 1:1 Help Now