Reputation: 1397
I have been reading "Effective Java" I didn't really follow the following argument. If someone could help me clear this
Abstract classes cannot be used to define mixins for the same reason that they can't be retrofitted onto existing classes:
especially "I didn't understand 'abstract classes can't be retrofitted onto existing class'"
could you please make a example.
http://books.google.se/books?id=ka2VUBqHiWkC&lpg=PA93&dq=Abstract%20classes%20cannot%20be%20used%20to%20define%20mixins%20for%20the%20same%20reason%20that%20they%20can't%20be%20retrofitted%20onto%20existing%20classes&pg=PA93#v=onepage&q=Abstract%20classes%20cannot%20be%20used%20to%20define%20mixins%20for%20the%20same%20reason%20that%20they%20can't%20be%20retrofitted%20onto%20existing%20classes&f=false
Thanks
Upvotes: 2
Views: 306
Reputation: 21793
Imagine if interfaces didn't exist in Java, but Java was still single inheritance.
Now imagine if you wanted to write a class that was a kind of stream, so you make it inherit from an existing Stream class to prevent having to rewrite all that code. Ah - but you also want your class to be serializable, so you go inherit from Serializ- wait a second... You're already inheriting from Stream. You can't inherit from that class, too. So, you're now in a difficult position where you have to rewrite a lot of code or just give up.
That's what's meant by 'abstract classes can't be retrofitted onto existing class' - if a class is in a certain type hierarchy, making it implement/inherit the behaviour of a new abstract class is impossible without rewriting the type hierarchy.
(I also agree with the comment that the book explains it perfectly clearly)
Upvotes: 2