[fpc-pascal] Inherited and not virtual/overridden procedures

Bart bartjunk64 at gmail.com
Wed Jun 12 00:12:39 CEST 2013


Hi,

I thought that if I had a subclass that inherited form a parentclass,
and both have a method with the same name (Bar), then the method of
the subclass hid the method of the parentclass, and you could not call
inherited Bar in the subclass.

More specifically I was under the impression that you could only call
inherited in an overriden method that was declared virtual in the
parentclass.

program test;

{$mode objfpc}{$H+}

uses
  SysUtils, Classes;

type
  TFoo = class
    procedure Bar;
  end;

  { TFooChild }

  TFooChild = class(TFoo)
    procedure Bar;
  end;

procedure TFoo.Bar;
begin
  writeln('TFoo.Bar');
end;

{ TFooChild }

procedure TFooChild.Bar;
begin
  inherited Bar;
  writeln('TFooChild.Bar');
end;


var
  FooChild: TFooChild;

begin
  FooChild := TFooChild.Create;
  FooChild.Bar;
  FooChild.Free;
end.

To my surprise this compiles without any warning or error.
It outputs:
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
TFoo.Bar
TFooChild.Bar

Why then would I need virtual and override anymore?

Bart



More information about the fpc-pascal mailing list