Imagine I have the following code:
class A: pass class B(A): pass class C(A): def __init__(self): self.b = B()
Is the above code correct in terms of correct inheritance? I mean is it a good practice to reference another subclass in a subclass?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.Sign up to join this community
It depends. In some languages (like Java), you don't really have a choice because every class (except
Object) is a subclass.
C needs some functionality from class
B to do its work, but you can't describe instances of class
C as also being instances of class
C can't work as a drop-in replacement for
B), then your design is absolutely correct.