?
Solved

About object and heritage

Posted on 2002-04-18
7
Medium Priority
?
156 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 150 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month12 days, 2 hours left to enroll

752 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