3 ms·
> For example, consider an "Animal" object class, which has a "numLegs" attribute. Then "Dog" could inherit from "Animal" by having exactly 4 legs, as a special
by magicalhippo 3y ago
> For example, consider an "Animal" object class, which has a "numLegs" attribute. Then "Dog" could inherit from "Animal" by having exactly 4 legs, as a special case.
In Delphi you can do this:
type
TAnimal = class abstract
public
class function NumLegs(): integer; virtual; abstract;
end;
TDog = class(TAnimal)
protected
class function NumLegs(): integer; override;
end;
{ TDog }
class function TDog.NumLegs: integer;
begin
result := 4;
end;
var
d: TDog;
begin
WriteLn(TDog.NumLegs); // 4
d := TDog.Create;
WriteLn(d.NumLegs); // 4
end.
I've used this several times to good effect. Were you thinking of something else? If so, could you expand a bit?