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" );
  }
}
5
  • 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. Commented Jun 6, 2014 at 9:09
  • can it actually be the case that Class A is in debug but Class B is not? Commented Jun 6, 2014 at 9:09
  • @MCEmperor thank you for the hint, I like it more when its explicit Commented Jun 6, 2014 at 9:13
  • @Moh-Aw yeah, every class should be able to debug itself independently Commented Jun 6, 2014 at 9: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 docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.4. Commented Jun 6, 2014 at 9:44

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Great explanation of why it's better to use composition rather than inheritance: stackoverflow.com/questions/49002/…
Thank you, I got around of my issue now. However I feel also stupid now for actually asking this..
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 :).
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.

1 Comment

Thank you, this would be my accepted answer if I would go with inheritance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.