0

I saw this approach recently and I can't get out of my head that this is not a good practice. Some of the reasons for that:

  1. dependency, if you add a new method in your interface all the others classes have to implement it;

  2. memory usage, (I'm not sure here) When instance it a object from this class the memory usage will be higher than if you just have an association relation;

I think that some languages don't allow this kind of feature for a reason but with Java I'm not so sure about it. Can anyone explain why we should implement multiple interfaces in a class or why we shouldn't ?

Thank you!

garyzhu
  • 13
  • 5
Valter Silva
  • 16,446
  • 52
  • 137
  • 218
  • 1
    1) How is that different than when it's implementing one interface? 2) Why would that increase the memory usage? – resueman Sep 11 '15 at 04:51
  • 1
    just google "SOLID" principle, hope this will help you. – BhushanK Sep 11 '15 at 04:57
  • 1
    One additional note: you can always *extend* an interface if you add new methods. This means you don't have to change *any* of the classes using the original interface if the implementation doesn't use/need/care about the new methods. – paulsm4 Sep 11 '15 at 06:12

4 Answers4

4

There's no thumb rule here and neither of the points you suggested applies. In fact your first point doesn't make sense, and your second point was just a wrong guess.

If you look at the JDK classes, you'll easily see some basis for how many (and which) interfaces a class implements. For example many of the collection classes implement at least Iterable and their respective collection interface (List, Set, Map, etc.).

It's a design choice, and while nothing stops you from creating a Runnable List, you would need to make a conscious choice to create something so bizarre.

The mentioned interface default methods are a mechanism that allowed the improvement of existing classes while keeping backwards compatibility (an important design aspect of Java).

Therefore it's not the amount of interfaces that counts when looking at a design.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
2

Implementing more interfaces has nothing to do with memory efficiency as compared to implementing one interface.

Implementing an interface (Look at What does it mean to "program to an interface"?) is a design by nature where you avoid multiple inheritance.

As you quote:

if you add a new method in your interface all the others classes have to implement it

Not all classes. Only the classes that you implement the interface with. It means that your objects need to have the common set of behaviors and implement it to their respective needs.

However, there are cases where in you don't need every classes need to implement the methods that the implemented interface requires. This had overcome in Java 8, where you have Default Methods that give default implementation.

Community
  • 1
  • 1
Karthik R
  • 5,523
  • 2
  • 18
  • 30
1

In response to your first point: in JDK 8 you can declare an interface method as default and provide an implementation within the interface, so that all implementing classes won't have to implement the new method (they can override though, if they like).

Some languages allow multiple superclasses of a class, but Java does not. Java therefore allows multiple interfaces due to this limitation.

Joobsies
  • 33
  • 5
1

First, You said "if you add a new method in your interface all the others classes have to implement it".

So, if you don't want to write like that you can use abstract class. I think that is not interface problem. That is about Desgin Patterns. You should learn about it. Multiple interfaces implementation are quite good if you understand OOP concept very well.

Zack Schulfler
  • 78
  • 1
  • 2
  • 11