1

In "Design Patterns - Elements of Reusable Object-Oriented Software" in chapter 1 in Section 1.6 under "Putting Reuse Mechanism to Work" in the "Inheritance versus Composition" part, the author is talking about drawbacks of inheritance:

Second, and generally worse, parent classes often define at least part of their subclasses' physical representation.

(emphasis by me)

What does physical representation mean in this context? I was doing some searching and found this stackoverflow question where in the answer physical was defined as memory is allocated for the object.

Does this mean when using inheritance the memory allocation of the object that inherits is partially defined by the object's parent? If it does, what does that mean in other words? And why is this a disadvantage?

hooni
  • 13
  • 4

1 Answers1

1

It's talking about two things:

  • Primarily, the implementation of the parent class methods (e.g., the parent class's code)
  • Also the memory layout of the instance fields defined by the parent class

The sentence is followed by:

Because inheritance exposes a subclass to the details of its parent's implementation, it's often said that "inheritance breaks encapsulation" [Sny86]. The implementation of a subclass becomes so bound up with the implementation of its parent class that any change in the parent's implementation will force the subclass to change.

That's directly referring to the implementation of the parent class methods.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875