* * *

Author Topic: Propagate to subclasses features added to a superclass  (Read 585 times)

simsee

  • New member
  • *
  • Posts: 34
Propagate to subclasses features added to a superclass
« on: February 22, 2017, 12:12:32 pm »
Dear all, I have problem concerning oop design. In an external library (with object pascal code available) I have a class hierarchy with a root class and some derived subclasses. I'd like to add some features (fields, properties and methods) to root class and to propagate them to existing derived subclasses. In order to achieve this goal, I have to modify the original code of library, or can I use some clever solutions (class helpers?, multiple inheritance via interfaces?)? Thanks!

Edson

  • Hero Member
  • *****
  • Posts: 710
Re: Propagate to subclasses features added to a superclass
« Reply #1 on: March 14, 2017, 08:47:40 pm »
FAIK, the only and safe way is to modify the root class.
Lazarus 1.6 - FPC 3.0.0 - x86_64-win64 on  Windows 7

simone

  • New member
  • *
  • Posts: 49
Re: Propagate to subclasses features added to a superclass
« Reply #2 on: March 15, 2017, 10:34:10 pm »
The pure OOP approach requires inheritance/subclassing to extend a class. So in this case the straight solution should be add fields/properties/methods to the root class. The price to pay for that would be the modification of code in the external library/component. If this is unacceptable or impossible, you can use a class helper for the root class, in order to add features that are then inherithed by subclasses. However class helpers have some limitations, among others in this case you can't add fields to the 'helped class', nor properties relying on backing field, but only methods and properties that don't refer to backing fields. This could be a solution for the problem. However notice that, in general, the use of class helpers by design is considered bad practice.
« Last Edit: March 16, 2017, 11:23:39 am by simone »

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus