Link to home
Start Free TrialLog in
Avatar of syloux
syloux

asked on

About object and heritage


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

Avatar of kretzschmar
kretzschmar
Flag of Germany image

guessing, its because its not overridden,
and mycar is not from the texpedition-class

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

meikl ;-)

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
Avatar of syloux
syloux

ASKER

Sorry, I do not understand the mechanism :(((
Know any site where I can find explanation ?
Procedure test ;
Var  MyCar: Tcar; <<< why not  TExpedition ?
Begin
  MyCar := TExpedition.Create;
  MyCar.drive;
End ;
ASKER CERTIFIED SOLUTION
Avatar of God_Ares
God_Ares

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
brummel . . .