• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Favor composition over inheritance

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

I'm reading "Design Patterns: Elements of Reusable Object-Oriented Software" book
in that i red below statements.

But class inheritance has some disadvantages, too. First, you can't change the implementations inherited from parent classes at run-time, because inheritance is defined
at compile-time. Second, and generally worse, parent classes often define at least part of their subclasses' physical representation.Because inheritance exposes a subclass to 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.



Please explain how inheritance exposes a subclass to details of its parent's implementation? and it mean?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

inheritance exposes a subclass to details of its parent's implementation



This statement does not mean in its literal sense i.e restricting the access to certain components. Here encapsulation refers to insulating the parent's implementation from the subclass. In inheritance the subclass becomes dependent on the parent class. Changes in the parent class can at times break the subclass. This leads us to conclude that the parent class's implementation is not encapsulated.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic