Implementing multiple interfaces with Java - is there a way to delegate?
Asked Answered
R

4

66

I need to create a base class that implements several interfaces with lots of methods, example below.

Is there an easier way to delegate these method calls without having to create a horde of duplicate methods?

public class MultipleInterfaces implements InterFaceOne, InterFaceTwo {

    private InterFaceOne if1;
    private InterFaceTwo if2;

    public MultipleInterfaces() {
      if1 = new ImplementingClassOne();
      if2 = new ImplementingClassTwo();
    }

    @Override
    public void classOneMethodOne { if1.methodOne(); }
    @Override
    public void classOneMethodTwo { if1.methodTwo(); }
    /** Etc. */


    @Override
    public void classTwoMethodOne { if2.methodOne(); }
    @Override
    public void classTwoMethodTwo { if2.methodTwo(); }
    /** Etc. */

}
Rough answered 28/12, 2010 at 14:36 Comment(0)
F
79

As said, there's no way. However, a bit decent IDE can autogenerate delegate methods. For example Eclipse can do. First setup a template:

public class MultipleInterfaces implements InterFaceOne, InterFaceTwo {
    private InterFaceOne if1;
    private InterFaceTwo if2;
}

then rightclick, choose Source > Generate Delegate Methods and tick the both if1 and if2 fields and click OK.

See also the following screens:

alt text


alt text


alt text

Frampton answered 28/12, 2010 at 14:47 Comment(2)
Thanks to BalusC and Lukas for such quick and useful answers. My project is based in Eclipse, so I was able to use BalusC's answer in near real-time ! Worked like a charm, and saved me several hours of frustrating work. As a new user of this forum, I am suprised and delighted at the quality of the participants here.Rough
Great hint! I didn't know that and will save 100's of hours in the future :) The good thing about stackoverflow: you'll also learn from questions you didn't ask yourself!Spermiogenesis
R
7

There is one way to implement multiple interface.

Just extend one interface from another or create interface that extends predefined interface Ex:

public interface PlnRow_CallBack extends OnDateSetListener {
    public void Plan_Removed();
    public BaseDB getDB();
}

now we have interface that extends another interface to use in out class just use this new interface who implements two or more interfaces

public class Calculator extends FragmentActivity implements PlnRow_CallBack {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

    }

    @Override
    public void Plan_Removed() {

    }

    @Override
    public BaseDB getDB() {

    }
}

hope this helps

Rodin answered 22/5, 2014 at 6:44 Comment(0)
S
5

Unfortunately: NO.

We're all eagerly awaiting the Java support for extension methods

Spermiogenesis answered 28/12, 2010 at 14:40 Comment(0)
O
0

There's no pretty way. You might be able to use a proxy with the handler having the target methods and delegating everything else to them. Of course you'll have to use a factory because there'll be no constructor.

Onstad answered 28/12, 2010 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.