Recent

Author Topic: Why do not descendant classes have fields on their own?  (Read 4270 times)

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Why do not descendant classes have fields on their own?
« on: March 17, 2017, 12:45:22 pm »
Somehow, I'm doing it wrong (again).
Code: Pascal  [Select][+][-]
  1. program mwe;
  2.  
  3. {$mode objfpc}
  4.  
  5. type
  6.         vehicle = class
  7.                 private
  8.                         wheelCount: word; static;
  9.         end;
  10.        
  11.         car = class(vehicle)
  12.         end;
  13.        
  14.         bus = class(vehicle)
  15.         end;
  16.  
  17. begin
  18.         car.wheelCount := 4;
  19.         bus.wheelcount := 6;
  20.        
  21.         writeLn('Cars have ', car.wheelCount, ' wheels.');
  22.         writeLn('Busses have ', bus.wheelCount, ' wheels.');
  23. 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.
Yours Sincerely
Kai Burghardt

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: Why do not descendant classes have fields on their own?
« Reply #1 on: March 17, 2017, 12:59:09 pm »
Why did you define wheelCount as static?

bytebites

  • Hero Member
  • *****
  • Posts: 625
Re: Why do not descendant classes have fields on their own?
« Reply #2 on: March 17, 2017, 12:59:43 pm »
Code: Pascal  [Select][+][-]
  1. program mwe;
  2.  
  3. {$mode objfpc}
  4.  
  5. type
  6.         tvehicle = class
  7.                 private
  8.                         wheelCount: word; //static;
  9.         end;
  10.        
  11.         tcar = class(tvehicle)
  12.         end;
  13.        
  14.         tbus = class(tvehicle)
  15.         end;
  16. var     car:tcar;
  17.         bus:tbus;
  18. begin
  19.         car:=tcar.create;
  20.         bus:=tbus.create;
  21.         car.wheelCount := 4;
  22.         bus.wheelcount := 6;
  23.        
  24.         writeLn('Cars have ', car.wheelCount, ' wheels.');
  25.         writeLn('Busses have ', bus.wheelCount, ' wheels.');
  26.         car.free;
  27.         bus.free;
  28. end.
  29.  

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: Why do not descendant classes have fields on their own?
« Reply #3 on: March 17, 2017, 01:46:35 pm »
Why 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)

[… some code …]
My first post said, I'm looking for an explanation.
« Last Edit: March 17, 2017, 01:49:05 pm by Kays »
Yours Sincerely
Kai Burghardt

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11353
  • FPC developer.
Re: Why do not descendant classes have fields on their own?
« Reply #4 on: March 17, 2017, 01:49:41 pm »
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

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Why do not descendant classes have fields on their own?
« Reply #5 on: March 17, 2017, 01:55:02 pm »
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)
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

  • Hero Member
  • *****
  • Posts: 6056
Re: Why do not descendant classes have fields on their own?
« Reply #6 on: March 17, 2017, 01:55:58 pm »
Why 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.

Quote
Static 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 :) )
« Last Edit: March 17, 2017, 02:03:03 pm by rvk »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11353
  • FPC developer.
Re: Why do not descendant classes have fields on their own?
« Reply #7 on: March 17, 2017, 02:30:49 pm »
Remove the static keyword and all works like you expected.

(well, if he actually would instantiate his classes, the current example would crash)

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: Why do not descendant classes have fields on their own?
« Reply #8 on: March 17, 2017, 04:18:52 pm »
Thanks for the answers so far. I'm getting a sense where this is going.
Yours Sincerely
Kai Burghardt

 

TinyPortal © 2005-2018