One of the recommended principles of object-oriented programming is the Liskov substitution principle : a subclass should behave the same as its base class (warning: this is actually not a correct description of the Liskov principle: see PS).
Is it also recommended to use constructors? Basically, I mean Python and its __init__() methods, but this question applies to any object-oriented language with inheritance.
I ask this question because it is sometimes useful to inherit a subclass from one or more classes that provide nice default behavior (for example, inheritance from a dictionary in Python, so obj['key'] works for objects of the new class). However, it is not always natural or simple to allow the use of a subclass in the same way as a dictionary: sometimes it would be better that the constructor parameters apply only to a specific user subclass (for example, a class that is a set of serial ports can behave like a dictionary with ports['usb1'] , which is USB port # 1, etc.). What is the recommended approach to this situation? having subclass constructors that are fully compatible with the constructions of their base classes and generate instances using the factory object function, which takes simple, user-friendly parameters? or just write a class constructor, the set of parameters of which cannot be directly set to the constructor of its base classes, but which is more logical from the user's point of view?
PS : I misinterpreted the Liskov principle, above: the Sven comment below indicates that subclass objects should behave like superclass objects (the subclass itself should not behave like a superclass, in particular, their constructors should not have the same parameters [signature]) .
source share