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
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
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
ASKER
Sorry, I do not understand the mechanism :(((
Know any site where I can find explanation ?
Know any site where I can find explanation ?
Procedure test ;
Var MyCar: Tcar; <<< why not TExpedition ?
Begin
MyCar := TExpedition.Create;
MyCar.drive;
End ;
Var MyCar: Tcar; <<< why not TExpedition ?
Begin
MyCar := TExpedition.Create;
MyCar.drive;
End ;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yep
brummel . . .
and mycar is not from the texpedition-class
TExpedition(MyCar).Drive; //should output the texpedtion.drive
meikl ;-)