Jump to content
bravesofts

the Real Purpose of using Reintroduce

Recommended Posts

the Purpose of reintroduce is to make a second introduce for same method from base class (not marked as virtual or dynamic)

with overload arguments in Drived Class whith inherited ofcourse ..
reintroduce let us add inherited in implementation of that overload method that reintroduced in Derrived Class
ex:
 

 type
   TBaseClass = class
   public
     constructor Create; // no virtual or dynamic
   end;
   
   TDerivedClass = class(TBaseClass)
   public
//     constructor Create; overload// introduced as overload but no arguments there !!
     constructor Create(aArg:T); reintroduce; overload;
   end;   
   
implementation

{ TBaseClass } 
 
 constructor TBaseClass.Create; begin
   // for example No code here !!
 end;
 
 { TDerivedClass } 
 
 constructor TDerivedClass.Create; 
 begin inherited Create; // reintroduce let us make inherited here 
                         //since base method is not marked as virtual or dynamic ..
  MyCode;                        
   // our method here will implement both (the inherited + MyCode)
   // but since the inherited is empty : the reintroduce here is Totally useless !!
   // the aim of reintroduce is to let us Merge the old method from base to reintroduce it 
   // as a new method in derived class using directive [inherited] 
   // does the overload necessary ?
   // if there is no necessary to add new arguments you can add just reintroduce to use 
   // the inherited directive there .. 
   // Is inherited mandatory when using reintroduce ?
   // a reintroduce without inherited is totally nosense !!
end;

finally : reintroduce is used especially for two Reasons:

  1. make inherited + overload with new parameters for same base method in Drived class
  2. make just inherited without overload it with new parameters
     

Q1)does reintroduce work also with base methods marked as virtual or dynamic ?

A1) do you heard about override ?
 

Q2) but if i need to introduce it with my custom parameters ?

A2) if that case ofcourse you need reintroduce; with overload;
 

Q3) is overload mandatory ?

A3) since reintroduce her second work is to hide any warnning compiler i can say yes the overload is a mandatory logically ..
-----
if i'm wrong plz tell me (Many thanks in Advance)

Aim of Reintroduce in Delphi png.png

  • Like 1

Share this post


Link to post

That post is extremely difficult to understand.

 

Documentation is here:

 

https://docwiki.embarcadero.com/RADStudio/Athens/en/Methods_(Delphi)#Overriding_versus_Hiding

 

17 minutes ago, bravesofts said:

the Purpose of reintroduce is to make a second introduce for same method from base class (not marked as virtual or dynamic)

No, the purpose of reintroduce is to suppress the compiler's warning that you are hiding the inherited method. It is a way for you to tell the compiler that you know what you are doing and to not bother you about it.

  • Like 9

Share this post


Link to post

ok , I totally agree with that definition in dokwiki..

 

but here:
https://stackoverflow.com/questions/53806/reintroducing-functions-in-delphi
 

plese someone tell me Why all this confusion in link above, about something that was created specifically just for that senarion in dokwiki?

 

atleast everyone there has a logic reason for that..

 

Please, I need to turn this page permanently...
Jim McKeeth said:

If you declare a method in a descendant class that has the same name as a method in an ancestor class then you are hiding that ancestor method  meaning if you have an instance of that descendant class (that is referenced as that class) then you will not get the behavior of the ancestor. When the ancestor's method is virtual or dynamic, the compiler will give you a warning.

Now you have one of two choices to suppress that warning message:

Adding the keyword reintroduce just tells the compiler you know you are hiding that method and it suppresses the warning. You can still use the inherited keyword within your implementation of that descended method to call the ancestor method.
If the ancestor's method was virtual or dynamic then you can use override. It has the added behavior that if this descendant object is accessed through an expression of the ancestor type, then the call to that method will still be to the descendant method (which then may optionally call the ancestor through inherited).
So difference between override and reintroduce is in polymorphism. With reintroduce, if you cast the descendant object as the parent type, then call that method you will get the ancestor method, but if you access it the descendant type then you will get the behavior of the descendant. With override you always get the descendant. If the ancestor method was neither virtual nor dynamic, then reintroduce does not apply because that behavior is implicit. (Actually you could use a class helper, but we won't go there now.)

In spite of what Malach said, you can still call inherited in a reintroduced method, even if the parent was neither virtual nor dynamic.

Essentially reintroduce is just like override, but it works with non-dynamic and non-virtual methods, and it does not replace the behavior if the object instance is accessed via an expression of the ancestor type.

Further Explanation:

Reintroduce is a way of communicating intent to the compiler that you did not make an error. We override a method in an ancestor with the override keyword, but it requires that the ancestor method be virtual or dynamic, and that you want the behavior to change when the object is accessed as the ancestor class. Now enter reintroduce. It lets you tell the compiler that you did not accidentally create a method with the same name as a virtual or dynamic ancestor method (which would be annoying if the compiler didn't warn you about).

is there a deep example where introducing each case with strong reason individually ? It’s essential for my overall well-being to close this chapter once and for all...

reintroduce in Delphi.png

Share this post


Link to post
Posted (edited)

The terminology in the quoted text is muddy, IMO.

 

Reintroduce does not affect the compiler other than suppressing a warning. You will get the same code emitted whether you omit the reintroduce keyword or include it. When you use the keyword you are just telling the compiler you intend to do something that kind of looks like a mistake (hiding the inherited method), because it is not common to declare a method in a child class that has the same name as a virtual/dynamic method in its parent and not also override it. (There are times where this makes sense but I personally try to avoid it because of the inevitable confusion it may cause down the road when it is forgotten this was done...) It's also sometimes the case that the inherited class changes: someone adds a new virtual method that didn't exist when the child class was designed, and now there is an unknown conflict; the warning is a good one so you can know this happened and deal with it accordingly.

 

I can't understand from your post what you actually want to do, so I can't offer you an alternative that works around the confusion.

 

 

Edited by Brandon Staggs
  • Like 6

Share this post


Link to post

I'm not sure if this thread is meant to be a question or actually a blog post...or tutorial.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×