Here's two simple classes, initially both have no keywords (virtual, overload, override, reintroduce):
TComputer = class(TObject)
public
constructor Create(Teapot: Integer);
end;
TCellPhone = class(TComputer)
public
constructor Create(Teapot: Integer; Handle: string);
end;
i will represent these above defintions as the slightly shorter:
TComputer = class(TObject)
constructor Create(Teapot: Integer);
TCellPhone = class(TComputer)
constructor Create(Teapot: Integer; Handle: string);
And when constructing TCellPhone
there is only one constructor (int
, string
) - because the ancestor constructor has been hidden. i will indicate the visible constructors of TCellPhone
as:
- Teapot: Integer; Handle: string
Now for the question, the first 3 cases make sense, the 4th does not:
1. Ancestor constructor is hidden by descendant:
TComputer = class(TObject)
constructor Create(Teapot: Integer);
TCellPhone = class(TComputer)
constructor Create(Teapot: Integer; Handle: string);
Teapot: Integer; Handle: string
This makes sense, the ancestor constructor is hidden because i've declared a new constructor.
2. Ancestor virtual constructor is hidden by descendant:
TComputer = class(TObject)
constructor Create(Teapot: Integer); virtual;
TCellPhone = class(TComputer)
constructor Create(Teapot: Integer; Handle: string);
Teapot: Integer; Handle: string
This makes sense, the ancestor constructor is hidden because i've declared a new constructor.
Note: Because the ancestor is virtual: Delphi will warn you that you're hiding the virtual ancestor (in the previous example of hiding a static constructor: nobody cares, so no warning). The warning can be suppressed (meaning "Yeah yeah yeah, i'm hiding a virtual constructor. i meant to do that.") by adding reintroduce:
TComputer = class(TObject) constructor Create(Teapot: Integer); virtual; TCellPhone = class(TComputer) constructor Create(Teapot: Integer; Handle: string); reintroduce;
3. Ancestor constructor not hidden in descendant because of overloading:
TComputer = class(TObject)
constructor Create(Teapot: Integer);
TCellPhone = class(TComputer)
constructor Create(Teapot: Integer; Handle: string); overload;
Teapot: Integer; Handle: string
Teapot: Integer
This makes sense, since the descendant constructor is an overload of the ancestor, so both are allowed to be present. The ancestor constructor is not being hidden.
4. Virtual ancestor constructor not hidden in descendant because overloading - but still get a warning:
This is the case that makes no sense:
TComputer = class(TObject)
constructor Create(Teapot: Integer); virtual;
TCellPhone = class(TComputer)
constructor Create(Teapot: Integer; Handle: string); overload;
Teapot: Integer; Handle: string
Teapot: Integer
Method 'Create' hides virtual method of base type 'TComputer'
This makes little sense. Not only is the ancestor not hidden, the descendant is overloaded; it shouldn't even be complaining.
What gives?