2

I come from a C++ background where multiple inheritance is not a problem. Hoewever in Java I'm constrained to use either only class to inherit from or implement an interface. I have multiple classes that already extends another class. But all these classes should share the same debug interface/class. How do I implement such behaviour without duplicating my debug logic multiple times?

Consider my current setup:

public interface Debug
{
  public abstract void log();
}

public class ClassA extends AnotherBaseClass implements Debug
{
  public boolean doDebug = false;

  public void log()
  {
    if( doDebug )
      System.out.println( "LOG" );
  }
}

public class ClassB implements Debug
{
  public boolean doDebug = false;

  public void log()
  {
    if( doDebug )
      System.out.println( "LOG" );
  }
}
Buni
  • 251
  • 2
  • 13
  • 3
    Notice that in Java, a method within an interface doesn't have to have the keyword `abstract`, because all methods are `abstract` by default. Also, the keyword `public` is unnecessary. – MC Emperor Jun 06 '14 at 09:09
  • can it actually be the case that Class A is in debug but Class B is not? – Moh-Aw Jun 06 '14 at 09:09
  • @MCEmperor thank you for the hint, I like it more when its explicit – Buni Jun 06 '14 at 09:13
  • @Moh-Aw yeah, every class should be able to debug itself independently – Buni Jun 06 '14 at 09:13
  • @Buni I know what you mean, but the Java Language Specification says otherwise: "It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface." See http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.4. – MC Emperor Jun 06 '14 at 09:44

2 Answers2

6

Typically this is where you use aggregation rather than inheritance, by having the classes have a member (typically private) they use for the debug logging. If the classes have to implement the interface as well as use debug logging internally, then you have them do that and then just hand off each of the Debug interface methods to the private instance.

If you're using Java 8, though, you have another option: see Pablo's answer for default inteface methods, which are kind of Multiple Inheritance Lite.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Great explanation of why it's better to use composition rather than inheritance: http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance – lpratlong Jun 06 '14 at 09:12
  • Thank you, I got around of my issue now. However I feel also stupid now for actually asking this.. – Buni Jun 06 '14 at 09:19
  • 1
    Do not feel stupid: this is a recurrent problem that most of developpers don't think about. They think inheritance is the best thing since they learn at school that inheritance is one of the principle of the OO. This is good to want to know what's better :). – lpratlong Jun 06 '14 at 09:32
4

If you are using Java 8 there is a new feature that addresses your requirement: you can use the reserved word default to provide a default implementation in the interface. This new feature is called default methods.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59