Difference between revisions of "Reintroduce"

From Free Pascal wiki
(important addition. Orginal state was not complete, not wrong , but incomplete)
m
Line 3: Line 3:
 
The [[modifier]] <syntaxhighlight lang="pascal" inline> reintroduce </syntaxhighlight> belongs to [[object-oriented programming]]. The modifier <syntaxhighlight lang="pascal" inline> reintroduce </syntaxhighlight> allows a method of the parent [[Class|class]] to be concealed by a new [[Method|method]] with the same name. That is, a new method exists in the class derived from the parent class and in all other classes derived from it. The method in the parent class is preserved and can still be used by it.
 
The [[modifier]] <syntaxhighlight lang="pascal" inline> reintroduce </syntaxhighlight> belongs to [[object-oriented programming]]. The modifier <syntaxhighlight lang="pascal" inline> reintroduce </syntaxhighlight> allows a method of the parent [[Class|class]] to be concealed by a new [[Method|method]] with the same name. That is, a new method exists in the class derived from the parent class and in all other classes derived from it. The method in the parent class is preserved and can still be used by it.
  
The method of the parent class does not exist anymore in the new class, it has been replaced by the new method with the same name. The method continues to exist in its original form in the parent class and can be used through the parent class. This is as opposed to [[override]] which only works for virtual methods. [[reintroduce]] merely suppresses a warning that a similar method already exists and that the programmer is aware of that.
+
The method of the parent class does not exist anymore in the new class, it has been replaced by the new method with the same name. The method continues to exist in its original form in the parent class and can be used through the parent class. This is as opposed to [[modifier]] override which only works for virtual methods. The [[modifier]] modifier merely suppresses a warning that a similar method already exists and that the programmer is aware of that.
  
 
Example:
 
Example:
Line 16: Line 16:
  
 
   TDerivedClass = class(TParentClass)
 
   TDerivedClass = class(TParentClass)
     procedure SetTest(strName: String); reintroduce; // This replaces the method of the parent class in the derived class. And supreeses warnings that a method with an identical signature already exists.
+
     procedure SetTest(strName: String); reintroduce; // This replaces the method of the parent class in the derived class. And supresses warnings that a method with an identical signature already exists.
 
   end;
 
   end;
  

Revision as of 14:22, 10 November 2019

Deutsch (de) English (en) suomi (fi) français (fr)

The modifier reintroduce belongs to object-oriented programming. The modifier reintroduce allows a method of the parent class to be concealed by a new method with the same name. That is, a new method exists in the class derived from the parent class and in all other classes derived from it. The method in the parent class is preserved and can still be used by it.

The method of the parent class does not exist anymore in the new class, it has been replaced by the new method with the same name. The method continues to exist in its original form in the parent class and can be used through the parent class. This is as opposed to modifier override which only works for virtual methods. The modifier modifier merely suppresses a warning that a similar method already exists and that the programmer is aware of that.

Example:

interface

type
  TParentClass = class
    procedure SetTest(intNum: Integer); // Some method
  end;

  TDerivedClass = class(TParentClass)
    procedure SetTest(strName: String); reintroduce; // This replaces the method of the parent class in the derived class. And supresses warnings that a method with an identical signature already exists.
  end;

implementation

procedure TDerivedClass.SetTest(strName: String);
begin
  inherited SetTest(1); // Call method with same name from parent class, if needed
end;