Recent

Author Topic: Procedure Inheritance  (Read 941 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 539
Procedure Inheritance
« on: December 02, 2022, 09:40:33 am »
Hello,

i have a Problem solving this:

i have a SuperClass and Some SubClasses. The Subclasses have an Init Procedure.
I have A List of Superclasses in my main Unit where i wanne use something like this:

Code: Pascal  [Select][+][-]
  1. for i:=0 to myList.Count - 1 do begin
  2.    mylist.Items[i].Initialize;
  3. end;
  4.  

however the Initialization is different in all of my Subclasses so i thought defining a procedure like this:

Code: Pascal  [Select][+][-]
  1. procedure Initialize; virtual;
  2.  

in my Superclass is pointless, because it will be an empty procedure. Can i Somehow predefine this Procedure in my Superclass to use it how i mentioned above ? I thought of creating an abstract procedure, but im not Sure if thats the right way.

Summarized Problem: the Procedure i wanna inherit from my Superclass, will be empty and i think thats a Pointless Procedure. Should i make an Abstract procedure for this or should i take another way to solve this ?

Can Someone please help me out, i'm overthinking here a lot i guess.

Thank you in advance :)
« Last Edit: December 02, 2022, 09:44:20 am by Weitentaaal »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Procedure Inheritance
« Reply #1 on: December 02, 2022, 09:58:27 am »
You could easy avoid all of this by doing your init inside of create?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Weitentaaal

  • Hero Member
  • *****
  • Posts: 539
Re: Procedure Inheritance
« Reply #2 on: December 02, 2022, 10:27:23 am »
You could easy avoid all of this by doing your init inside of create?

Thought of that too but i have a Lot of Properties to set and the Signature of the constructor would get realy long. The SubClasses will get initialized by another object.

Edit: Off Topic but why does it say « Last Edit: Tomorrow at 31:76:97 by KodeZwerg » on your post ?  :D
« Last Edit: December 02, 2022, 10:28:57 am by Weitentaaal »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Procedure Inheritance
« Reply #3 on: December 02, 2022, 10:35:09 am »
You could easy avoid all of this by doing your init inside of create?
Thought of that too but i have a Lot of Properties to set and the Signature of the constructor would get realy long. The SubClasses will get initialized by another object.

When signature differs from class to class, I dont think that iterating over array would work well... but I would need to see some real example of your class and subclasses to find a good solution.

Edit: Off Topic but why does it say « Last Edit: Tomorrow at 31:76:97 by KodeZwerg » on your post ?  :D
Its my signature to bring a smile on faces  :P
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Weitentaaal

  • Hero Member
  • *****
  • Posts: 539
Re: Procedure Inheritance
« Reply #4 on: December 02, 2022, 11:09:11 am »

When signature differs from class to class, I dont think that iterating over array would work well... but I would need to see some real example of your class and subclasses to find a good solution.


Thats why i wanted this Initialization procedure. It is virtual so my Subclasses can override it. Otherwise i would need something like this:

Code: Pascal  [Select][+][-]
  1. //Example with Cars
  2. TMyCar = Class;
  3.  
  4. TVolvo = Class;
  5.  
  6. TVolkswagen = Class;
  7.  
  8. //Mainform
  9. var
  10.    ListOfCars: TMyCarList; //(wich will be a TFPGObjectList)
  11. begin
  12.    for i:=0 to ListOfCars.Count - 1 do begin
  13.       ListOfcars.Items[i].Initialize;
  14.    end;
  15.  
  16. //Another Example: i create a Car and add it to a PrakingLot which, again, is a list
  17. Volvo:= TVolvo.Create;
  18.  
  19. ListOfCars.Add(Volvo);
  20.  
  21.  

And in General, if it wouldn't be an initialization process. How would i do this with a normal Procedure. The Superclass procedure would be Empty and overriden in the SubClasses. But i think an Empty Procedure Should be avoided right ?

Quote
Its my signature to bring a smile on faces  :P

u got me there XD. Thought u were in a Different Timezone but when i saw "at 31:76:97", my brain just lagged :D.

Zvoni

  • Hero Member
  • *****
  • Posts: 2691
Re: Procedure Inheritance
« Reply #5 on: December 02, 2022, 12:26:22 pm »
From the doc's: https://www.freepascal.org/docs-html/ref/refsu22.html
Quote
An abstract method is a special kind of virtual method. A method that is declared abstract does not have an implementation for this method. It is up to inherited objects to override and implement this method.

From this it follows that a method can not be abstract if it is not virtual (this can be seen from the syntax diagram). A second consequence is that an instance of an object that has an abstract method cannot be created directly.
Question: Are you ever creating a direct instance of the Superclass? If not, then Abstract might be an approach, if Yes, there is only virtual left
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

jamie

  • Hero Member
  • *****
  • Posts: 6579
Re: Procedure Inheritance
« Reply #6 on: December 02, 2022, 12:28:23 pm »
Then there is always the IINTERFACE  :D
The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 2691
Re: Procedure Inheritance
« Reply #7 on: December 02, 2022, 12:59:38 pm »
Then there is always the IINTERFACE  :D
Ah yes.....forgot that one.
Which might even be the better solution. Because if you pass any of the Subclasses to a procedure via Super-Class argument, the object itself would still know which Init-procedure to call
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

PascalDragon

  • Hero Member
  • *****
  • Posts: 5678
  • Compiler Developer
Re: Procedure Inheritance
« Reply #8 on: December 02, 2022, 05:23:59 pm »
however the Initialization is different in all of my Subclasses so i thought defining a procedure like this:

Code: Pascal  [Select][+][-]
  1. procedure Initialize; virtual;
  2.  

in my Superclass is pointless, because it will be an empty procedure. Can i Somehow predefine this Procedure in my Superclass to use it how i mentioned above ? I thought of creating an abstract procedure, but im not Sure if thats the right way.

If you override Initialize in all of your sub classes you can declare the one in your superclass as abstract. If not, just leave the virtual method in your base class empty and be done with it (e.g. TObject.AfterConstruction and TObject.BeforeDestruction are empty as well). No need to complicate things any further.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 539
Re: Procedure Inheritance
« Reply #9 on: December 05, 2022, 07:36:45 am »
I will try to use an Interface here, otherwise i will let the Method empty as @PascalDragon suggested!

Thank you Guys for your Feedback !  :)

 

TinyPortal © 2005-2018