0

I am reading a book where I dont understand below 3 problems mention about inheritance :

1 ) First, you can't change the implementations inherited from parent classes at run-time, because inheritance is defined at compile-time.

2) 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".

3)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.

All the above points was mention without any code snippet so I am having problem understanding above 3 points.

I would really appreciate if someone could help me understand above 3 points with code example and how in code each of the points impact and where.

I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • Stackoverflow is for code questions so maybe you can try out your own examples with some simple coding experiments? You might even arrive at some answer, just by doing. – Kokodoko Jun 25 '19 at 11:39
  • 1
    @Kokodoko I somewhat disagree since there are lots of question on SO like this.For instance how about this(https://stackoverflow.com/questions/6308178/what-is-the-main-difference-between-inheritance-and-polymorphism) – I Love Stackoverflow Jun 25 '19 at 11:55
  • Which book is this from? – Wim Coenen Jun 25 '19 at 12:12
  • @WimCoenen It is from : Design Patterns,Element of Reusable Object-Oriented Software . It is available online in form of pdf – I Love Stackoverflow Jun 25 '19 at 12:19
  • If they don't provide examples to prove - or at least illustrate - their points, it's not a very good book, in my opinion. – 500 - Internal Server Error Jun 25 '19 at 12:43
  • @ILoveStackoverflow not all questions include code, but it really helps to try and write a little program for yourself. It might all fall into place and that's really how you learn. Just a tip! – Kokodoko Jun 25 '19 at 12:47
  • @500-InternalServerError Actually it 2nd most popular book as per SO votes (http://www.dev-books.com/) – I Love Stackoverflow Jun 25 '19 at 12:49
  • 1
    @Kokodoko I really appreciate your tip but in this case I really didnt understand those 3 points to write some code that is why I have asked here – I Love Stackoverflow Jun 25 '19 at 12:57

1 Answers1

0

1 ) First, you can't change the implementations inherited from parent classes at run-time, because inheritance is defined at compile-time.

This means that the inheritance relation between the two classes is established in the code you write and after compilation, when the code executes, this cannot be changed.

2) 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".

If you subclass some parent, you usually mean the subclass is also like the parent with some extensions and/or overriding differences. Some properties or behaviour is common and you have access to this. There is some degree of shared knowledge between the two and you can think of this as information leakage. If encapsulation were to be preserved at all costs you are breaking it (but for good reasons). If it were not like this inheritance would be useless.

3)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.

I don't have the context of the three clauses, but yes, they have to be coupled to a certain degree, how much changes in the parent affect the subclass it depends on the concrete case.

progmatico
  • 4,714
  • 1
  • 16
  • 27