0

Say I have several interfaces that all extends from a parent interface. The parent interface has some members that must be used by all child interfaces, so normally I would like to add a constructor in the parent interface to setup these common members. But this cannot be done as the classes are interfaces, not abstract classes. And I cannot simply change them to abstract classes because I would allow a class to implement multiple interfaces, which cannot be done using abstract classes. How do you resolve this yet ensuring the design requirements?

e.g.

interface Parent {
    SomeCommonMember commMember;
    Parent(SomeParameter para) {
        // do some calculations to init commMember based on para
        commMember = ...;
    }
}

interface ChildA extends Parent {
    void ChildAMethod();
}

interface ChildB extends Parent {
    void ChildBMethod();
}

public class MyImplementation implements ChildA, ChildB {
    MyImplementation(SomeParameter para) {
        super(para);
    };
    void ChildAMethod() {
        // uses commMember
    };
    void ChildBMethod() {
        // uses commMember
    };
}

public class Test {
    public static void main(String[] args) {
        Parent generic = new MyImplementation(new SomeParameter());
        if (generic instanceof ChildA) {
            generic.ChildAMethod();
        }
        if (generic instanceof ChildB) {
            generic.ChildBMethod();
        }
    }
}
user1589188
  • 5,316
  • 17
  • 67
  • 130

2 Answers2

2

If you are using Java 8, you can define default methods to the interface. But note that you cannot have properties at the interface level. So you would have to modify your process for this.

Simon
  • 2,353
  • 1
  • 13
  • 28
1

I think the only thing you can do to ensure each implementation of the Parent interface has the common members is to define the getters and the setters for these common members (and additional methods regarding these members) in the Parent interface. This way, you're sure each implementation of the interface will have something that represents the common member and that you can get it, set it (and the other stuff you defined as default in the parent interface).

So you'll have:

interface Parent {

    //getter
    CommonMember getCommonMember();

    //setter
    void setCommonMember(CommonMember newValue);

    //if needed, additional mandatory method for processing common member
    void processCommonMember();
}

In the implementing classes you can then use these methods. You'll still have some typework (implementing the same methods) but that's the price to pay when using interfaces ... If you really don't want to retype all that stuff you'll have to go with abstract classes and use aggregation or composition to try to "imitate" the extension of multiple (abstract) class:

class A {
   //some methods
} 

class B {
   //some methods
}

class AwithB {
   private A a;
   private B b;

   //implement methods of A using a
   ...

   //implement methods of B using b
   ...
}

But you're kind of limited (on another way) with this option too...

J.Baoby
  • 2,167
  • 2
  • 11
  • 17