Search code examples
javaclassinterfacecompositionobject-composition

Multiple Inheritance, using interface vs using composition in Java


Based on some stackoverflow post:

Java does not support multiple inheritance.

There are a few workarounds I can think of:

The first is composition: make a class that takes those two activities as fields.

The second is to use interfaces. // This is my question

I try constructing a class that can have AA class's method and BB class's method through interface or composition. I read a lot about inheritance vs composition, but my question is interface vs composition (which one is better). If I want to use these two classes methods in a single class Preference,

class AA{
  public int id(){
      return 0;
  }
}

class BB{
  public String name(){
      return "0";
  }
}

Obviously, Java does not support multiple inheritance and we need to use either interface or composition.

Interface:

First Construct interfaces that corresponds to these two classes:

interface AAInterface{
  public int id();
}

interface BBInterface{
  public String name();
}

then construct a Preference class that implements those two interfaces, and declared two class objects inside that class:

class Preference implements AAInterface, BBInterface{
  AA aa; 
  BB bb;

  public int id() {
      return aa.id();
  }

  public String name() {
      return bb.name();
  }
}

Composition: Directly construct a class that has two fields that corresponds to two class objects in order to use their methods.

class Preference{
  AA aa; 
  BB bb;

public int id() {
    return aa.id();
  }

public String name() {
    return bb.name();
  }
}

As a result, in the Preference class, by using other interface or composition method, I can use the id() method and the name() method which derived from AA class and BB class.

Question: Why should we use interface since composition is way simpler and better? Are there any reason that we should use the interface instead of composition? To put it differently, how to get two classes methods in a single class using java interface, is my way the correct way to do that?


Solution

  • Interfaces are not about inheriting behaviour. Make your class implement an interface if you want to let OTHERS know what your class is capable of, not to teach YOUR class how to do stuff.

    If some library can do some interesting stuff to an object that has a getName() method, you cannot expect that it knows all the classes in the world that have this method, but you can make your class implement an interface that the library knows (say Named), which enforces this method is present.

    From your perspective, it does not modify your class much, and your objects may still be vastly different from other Named objects. From that library's perspective, it makes your object usable.

    To elaborate: if you only need access to specific methods that already exist in some other class, this is enough:

    class Preference{
      AA aa; 
      BB bb;
    
    public int id() {
        return aa.id();
      }
    
    public String name() {
        return bb.name();
      }
    }
    

    Your object will know how to say it's name and id (or will it? It's not really your object's name after all :) . But you will not, among other things, be able to add it to collection of AAs, BBs, AAInterfaces or BBInterfaces. It IS neither of them. It just acts like them. Depending on the needs it may or may not be enough.