Procedure Inheritance lazarus / 21-General ---------------------------------------------------------------------- Weitentaaal · 2022-12-02 08:40 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for i:=0 to myList.Count - 1 do begin   mylist.Items[i].Initialize;end;  however the Initialization is different in all of my Subclasses so i thought defining a procedure like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Initialize; virtual;  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 ---------------------------------------------------------------------- KodeZwerg · 2022-12-02 08:58 You could easy avoid all of this by doing your init inside of create? ---------------------------------------------------------------------- Weitentaaal · 2022-12-02 09:27 Quote from: KodeZwerg on December 02, 2022, 09:58:27 amYou 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 ? ---------------------------------------------------------------------- KodeZwerg · 2022-12-02 09:35 Quote from: Weitentaaal on December 02, 2022, 10:27:23 amQuote from: KodeZwerg on December 02, 2022, 09:58:27 amYou 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. Quote from: Weitentaaal on December 02, 2022, 10:27:23 amEdit: Off Topic but why does it say « Last Edit: Tomorrow at 31:76:97 by KodeZwerg » on your post ?  Its my signature to bring a smile on faces ---------------------------------------------------------------------- Weitentaaal · 2022-12-02 10:09 Quote from: KodeZwerg on December 02, 2022, 10:35:09 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //Example with CarsTMyCar = Class; TVolvo = Class; TVolkswagen = Class; //Mainformvar   ListOfCars: TMyCarList; //(wich will be a TFPGObjectList)begin   for i:=0 to ListOfCars.Count - 1 do begin      ListOfcars.Items[i].Initialize;   end; //Another Example: i create a Car and add it to a PrakingLot which, again, is a listVolvo:= TVolvo.Create; ListOfCars.Add(Volvo);   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 ? QuoteIts my signature to bring a smile on faces  u got me there XD. Thought u were in a Different Timezone but when i saw "at 31:76:97", my brain just lagged . ---------------------------------------------------------------------- Zvoni · 2022-12-02 11:26 From the doc's: https://www.freepascal.org/docs-html/ref/refsu22.html QuoteAn 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 ---------------------------------------------------------------------- jamie · 2022-12-02 11:28 Then there is always the IINTERFACE ---------------------------------------------------------------------- Zvoni · 2022-12-02 11:59 Quote from: jamie on December 02, 2022, 12:28:23 pmThen there is always the IINTERFACE  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 ---------------------------------------------------------------------- PascalDragon · 2022-12-02 16:23 Quote from: Weitentaaal on December 02, 2022, 09:40:33 amhowever the Initialization is different in all of my Subclasses so i thought defining a procedure like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Initialize; virtual;  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 · 2022-12-05 06:36 I will try to use an Interface here, otherwise i will let the Method empty as @PascalDragon suggested! Thank you Guys for your Feedback !