Solved

About object and heritage

Posted on 2002-04-18
7
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

726 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