1

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?

SHE
  • 121
  • 3
  • 11
  • 1
    ```void someMethod(AAInterface param)``` <-- can not pass the composed ```Preference```. – Jorn Vernee Jun 05 '16 at 22:05
  • You haven't explained what the end result is. For example, if you'd like to be able to treat a `Preference` in the same way as an `AA` and a `BB`, then an interface makes a lot of sense. If you don't need to do that, then don't... – Jon Skeet Jun 05 '16 at 22:05
  • I want to use those methods in a single class, that is my objective @JonSkeet – SHE Jun 05 '16 at 22:08
  • 1
    Sorry, that's still not nearly enough context. I'm off to bed now anyway, but I strongly urge you to rewrite your question after reading https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ – Jon Skeet Jun 05 '16 at 22:08
  • I did not get it @JornVernee – SHE Jun 05 '16 at 22:14
  • 1
    @SHE This seems like an [XY-Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) (and is most likely the reason why Jon Skeet asked about the end result). Please describe what you are trying to do instead of your possible solutions. We can give you a better solution or a clearer answer to your question if we understand your problem. – Turing85 Jun 05 '16 at 22:15
  • My question is how to use interface to do "multiple inheritance" in Java? Is my way the correct way? – SHE Jun 05 '16 at 22:26
  • @SHE again the question: what is your use case? As was mentioned in the very first comment of Jorn Vernee and the answer of JockX, you can "inform" other methods of your functionality by `implements Interface`. Interfaces have some other benefits, e.g. you can create algorithms that only work on interfaces (and are therefore somewhat future-proof) or to inject concrete implementations at run-time. – Turing85 Jun 05 '16 at 22:36
  • alright @Turing85..... – SHE Jun 05 '16 at 22:37

1 Answers1

1

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.

JockX
  • 1,928
  • 1
  • 16
  • 30
  • "Interfaces are not about inheriting behaviour." The sole motivation of interfaces is inheritance, or - to be more precise - the lack of multi-inheritance in Java. Multi-inheritance and interfaces can be seen as somewhat redundant (multi-inheritance is more powerful, but you do not need interfaces if you have mulit-inheritance and abstract/virtual methods). And since the advent of Java 8, [you can inherit behaviour from interfaces](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html). – Turing85 Jun 05 '16 at 22:25
  • 3
    @Turing85 I disagree. The sole motivation of interfaces is to describe a contract of *behavior* without implementation. An interface can *extend* another interface with more behavior. That is extension, not inheritance, since no implementation logic was inherited, e.g. like a `List` and a `Set` behave differently, but both behave *like* a `Collection`. – Andreas Jun 05 '16 at 22:41
  • @Andreas You can describe a contract with a pure virtual/abstract class just as well (that is the way it is done in C++). In Java, it is normally done by an interface since you do not know whether a class implementing your contract already uses the inheritance-relationship. As for the "inherti behaviour" part: I think you misunderstood me. I am well aware what `extends` on interface-level means. My comment was made with default methods in mind. – Turing85 Jun 05 '16 at 22:46
  • You *can* do a lot of things. I was talking about *motivation*, not how language features can be (mis)used. The *primary* purpose of interfaces is describing contract of behavior. Interface inheritance (aka extensions) are secondary to that. It seems we both misused the word "sole", because that is more than one motivation. – Andreas Jun 05 '16 at 22:53
  • 1
    @Turing85 I think the bottom line, and I assume the core of OP 's question, is that SHE wants the code available from the new class, without much coding or repetition. So neither interface nor virtual implementation is immediately helpful, IMO – JockX Jun 05 '16 at 22:53
  • @Andreas: I like the way you describe interfaces as contracts for behavior (rather than as units of behavior). Although I believe this is the correct way to regard interfaces, Java 8 has further blurred (but has not erased) the distinction between interfaces and classes by allowing interfaces to contain default implementations and static methods. – scottb Jun 06 '16 at 00:10