Disadvantages of inheritance in java

Can anyone explain the lack of inheritance in java

+6
java inheritance
source share
5 answers

you probably want to read:

Effective Java ™ Second Edition Posted by Joshua Bloch

Chapter 4. Classes and Interfaces

Item 16: Etching of a composition over inheritance

Paragraph 17: Design and document for inheritance or prohibition on it

Item 18: Preferred Interfaces for Abstract Classes

+10
source share

Take the booty in an Allen Holub article in JavaWorld entitled Why the extension is evil . He discusses such things as tight communication and the problem of a fragile base class .

+5
source share

If your class is not intended to be inherited, it should be final. You have to be very careful to make sure that you understand how the methods to be overridden in a subclass that was not intended for the inheritance function, in order to understand how to modify them.

Specific example:

You have a class that manages a list of names ...

public class MyNameManager { private List<String> numbers = new LinkedList<String>(); public void add(String value) { numbers.add(value); } public void addAll(Collection<String> values) { for(String value : values) { add(value); } } public void remove(String value) { //... } //... } 

Now say that you want to create a new subclass that also counts the total number of times the name is added to the list, for example:

 public class MyCountingNameManager extends MyNameManager { private int count = 0; @Override protected void addAll(Collection<String> values) { count += values.size(); super.addAll(values); } @Override protected void add(String value) { count += 1; super.add(value); } } 

Seems pretty simple, no? But consider the result of the following:

 MyCountingNameManager m = new MyCountingNameManager(); m.add("bob"); m.add("Sally"); 

The score is now 2, and all is well. But if we did the following:

 List<String> family = new List<String>(); family.add("mom"); family.add("dad"); family.add("brother"); MyCountingNameManager m = new MyCountingNameManager(); m.add(family); 

The score is now 6, not 3, which you probably expect. This is because calling addAll adds the size of the collection of values ​​(3) to the account, and then calls the super.addAll method to do the actual processing. super.addAll through the collection and calls the add method for each value. But since we are working with MyCountingNameManager and not a MyNameManager , each time the method is called, it overrides add in the subclass. The MyCountingNameManager.add method, which runs, also increments the counter! Thus, each name is counted twice!

I believe this example comes from Effective Java. You should definitely find a copy and read the elements listed in Viele's answer in order to better understand some cases where inheritance is poorly suited.

+3
source share

We prefer composition over inheritance, because when we add (in particular) or change functionality by subclassing, we associate this new functionality with a class - therefore, wherever we need new functions, we need this class. This extends even to subsequent subclasses - and with the same Java inheritance model, if we have two new functionality bits that we want in another class, there is no (simple) way to enter both bits if each of them is in a separate subclass of the original ancestor .

In comparison, if we extend functionality with composition, any class - from or from our existing hierarchy - can include it by simply including a tiny class that has a new function. It is simpler, cleaner, more reusable and easier to read.

Inheritance has its place, but it is not the right tool for many, many tasks.

+2
source share

Inheritance provides closely related relationships between classes and carries a lot of weight compared to runtime polymorphism interfaces.

0
source share

All Articles