Reputation: 67
I'm learning free pascal using the Lazarus IDE and I don't know how to inheritance methods in the derived form.
I want something like this:
Form base or father:
procedure HelloWorld;
begin
ShowMessage('Hello World from base form or father');
end;
and form derived or child:
procedure HelloWorld;
begin
inherited;
ShowMessage('Hello World from derived form or child');
end;
I want the result shows 2 messages by clicking (e.g Button1)
Thanks!!!
Upvotes: 1
Views: 2176
Reputation: 449
For a better appreciation of the Object Pascal language, i believe you should start by reading the freepascal reference guide. FreePascal is the underlaying compiler below lazarus.
Its important to understand that Forms, Labels, Buttons, etc, are specific incarnations of the concepts of objects, instances, classes etc.
In that regard, a class is a structure binding code and data. What you want to achieve is something like this :
Type
TMyClass = Class(<ancestorclass>)
<fields and methods>
End;
TMyChildClass = Class(TMyClass)
<fields and methods>
End;
This means that TMyChildClass is a class derived from TMyClass. In the event that you have methods in both classes with same name, you can use the keyword "override" to show the compiler that that method was overriden by the child class, like this :
TMyClass = Class /* No parenthesis or ancestor name means the class derives from TObject */
Procedure ParentMethod;
End;
TMyChildClass = Class(TMyClass)
Procedure ParentMethod; Override;
End;
Procedure TMyClass.ParentMethod;
Begin
DoSomething;
End;
Procedure TMyChildClass.ParentMethod; /* Dont repeat the override */
Begin
Inherited; // This will call the parents method
End;
This is the proper way to do method override in object pascal. If the definition of the class where you want to use "inherited" has no parenthesis and the name of the ancestor class, theres no ancestry relation between then and inherited will not do what you are expecting to do.
Upvotes: 4
Reputation: 3669
In Pascal procedure
is not an object oriented programming construct.
FreePascal includes objects and objects can include procedures:
Upvotes: 2