Can a form inherit from a superclass (non-form) ? lazarus / 39-Graphics ---------------------------------------------------------------------- Weitentaaal · 2021-08-30 07:00 Hello , I Just wanted to ask how i could realize the following: Superclass: SuperC = class(TObject) SubClass MyForm = class(TForm) --> Should inherit from the "SuperC" I have several forms, which share some properties. Therefore I wanted to create a superclass that initializes the variables of the shapes. Is it possible to do this with inheritance ? Since the form can't inherit from TForm and another class I don't know exactly which solution would be best here. Of course I could just manually assign the variables to the class each time, but this would not be such a nice solution in my eyes. Maybe some tips from professionals? ###################################################################### ## Edit: I realized that a class can derive from TForm even if it has no form itself. So now my "Data" class can initialize all forms. Should hopefully not give any peroblems later (haven't tested everything). If someone has a better solution I would be very happy if you could share it here. Superclass: TCars = class(TForm) --> Does not have a Form TLocations = (LocGermany, LocRussia); SubClass TMyCar= class(TCars ) TMyTruck= class(TCars ) TMyMotorcycle= class(TCars ) Array of Components (Example) Traffic : Array of TCars; Traffic[1] := TMyCar.Create Traffic[2] := TMyTruck.Create Traffic[3] := TMyMotorcycle.Create Traffic[1].SetGoal := LocGermany Traffic[1].SetStart := LocRussia Traffic[1].Drive(); Thanks ---------------------------------------------------------------------- Zvoni · 2021-08-30 08:14 Yes, but why would you derive your Super-Class from TObject? TObject is the common Ancestor for all classes, even TForm Derive it from TForm. You can write your own constructor for your Superclass. ---------------------------------------------------------------------- Weitentaaal · 2021-08-30 08:30 Thanks Zvoni, but would my Example be a proper Solution ? or how should i approach something like this ? Quote from: Zvoni on August 30, 2021, 10:14:39 amYes, but why would you derive your Super-Class from TObject? TObject is the common Ancestor for all classes, even TForm Derive it from TForm. You can write your own constructor for your Superclass. "but why would you derive your Super-Class from TObject?" I knew that TObject was the Ancestor of TForm but u have to write Something in there or don't u have to do that ? TCars = class()  --> So i could just write this instead ? "You can write your own constructor for your Superclass." I did thanks a lot ---------------------------------------------------------------------- Zvoni · 2021-08-30 09:02 Quote from: Weitentaaal on August 30, 2021, 10:30:14 amTCars = class()  --> So i could just write this instead ? No, in that case it would inherit from TObject and not TForm. ---------------------------------------------------------------------- wp · 2021-08-30 09:07 Put the common code into an ancestor of the forms used by your application: - TBaseForm = class(TForm) --> all common code - TMainForm = class(TBaseForm) --> all common code + code introduced by TMainform - TSecondaryForm = class(TBaseForm) --> all common com + code introduced by TMainForm - etc. ---------------------------------------------------------------------- Weitentaaal · 2021-08-30 10:34 Thanks for your effort, much appreciated. Everything working now