Why do not descendant classes have fields on their own? lazarus / 61-Beginners ---------------------------------------------------------------------- Kays · 2017-03-17 11:45 Somehow, I'm doing it wrong (again).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 program mwe; {$mode objfpc} type        vehicle = class                private                        wheelCount: word; static;        end;              car = class(vehicle)        end;                bus = class(vehicle)        end; begin        car.wheelCount := 4;        bus.wheelcount := 6;                writeLn('Cars have ', car.wheelCount, ' wheels.');        writeLn('Busses have ', bus.wheelCount, ' wheels.');end.Why do both cars and busses end up having six wheels? I thought at the end of the inheritance tree you tie all ancestor definitions and methods together to one independent type. However here both leaves don't exist on their own. Their shared ancestor does not become part of their definition, but is just referenced. I'm looking for an explanation here. I guess generics are a solution in my case. ---------------------------------------------------------------------- rvk · 2017-03-17 11:59 Why did you define wheelCount as static? ---------------------------------------------------------------------- bytebites · 2017-03-17 11:59 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 program mwe; {$mode objfpc} type        tvehicle = class                private                        wheelCount: word; //static;        end;               tcar = class(tvehicle)        end;               tbus = class(tvehicle)        end;var     car:tcar;        bus:tbus; begin        car:=tcar.create;        bus:=tbus.create;         car.wheelCount := 4;        bus.wheelcount := 6;               writeLn('Cars have ', car.wheelCount, ' wheels.');        writeLn('Busses have ', bus.wheelCount, ' wheels.');        car.free;        bus.free;end. ---------------------------------------------------------------------- Kays · 2017-03-17 12:46 Quote from: rvk on March 17, 2017, 12:59:09 pmWhy did you define wheelCount as static? because all cars/busses have n wheels, not just one instance of a car/bus has n and others may have m wheels (where n <> m) Quote from: bytebites on March 17, 2017, 12:59:43 pm[… some code …]My first post said, I'm looking for an explanation. ---------------------------------------------------------------------- marcov · 2017-03-17 12:49 You define a variable associated with the tvehicle class type. But when you access it via tcar or tbus, you expect it to suddenly be different polymorphic.  It is stilll TVehicle.wheelcount. ---------------------------------------------------------------------- Leledumbo · 2017-03-17 12:55 Quote from: Kays on March 17, 2017, 01:46:35 pmbecause all cars/busses have n wheels, not just one instance of a car/bus has n and others may have m wheels (where n <> m) You misunderstood what the static modifier does. That way, you are declaring shared field across ALL instances, not specific to any descendant. There's no way in OOP to have a shared field declared in parent class, but behaves as if it's only shared among instances of the same descendant. ---------------------------------------------------------------------- rvk · 2017-03-17 12:55 Quote from: Kays on March 17, 2017, 01:46:35 pmQuote from: rvk on March 17, 2017, 12:59:09 pmWhy did you define wheelCount as static? because all cars/busses have n wheels, not just one instance of a car/bus has n and others may have m wheels (where n <> m) That's not what the "static" stands for. Static is the same as defining a "class var Wheelcount" which means it is defined as "global variable" for all instances. QuoteStatic Keyword Declaring class members or methods as static makes them accessible without needing an instantiation of the class. So see the wheelCount as a globale variable and you know why it's changed for all instances. Remove the static keyword and all works like you expected. (edit: Leledumbo beat me to it ) ---------------------------------------------------------------------- marcov · 2017-03-17 13:30 Quote from: rvk on March 17, 2017, 01:55:58 pmRemove the static keyword and all works like you expected. (well, if he actually would instantiate his classes, the current example would crash) ---------------------------------------------------------------------- Kays · 2017-03-17 15:18 Thanks for the answers so far. I'm getting a sense where this is going.