Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. */  } 
like image 855
Chuck Mosher Avatar asked Dec 28 '10 14:12

Chuck Mosher


People also ask

Can you implement multiple interfaces in Java?

Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. By convention, the implements clause follows the extends clause, if there is one.

Can we inherit from multiple interfaces?

One of the core principles of Object-Oriented Programming – inheritance – enables us to reuse existing code or extend an existing type. Simply put, in Java, a class can inherit another class and multiple interfaces, while an interface can inherit other interfaces.

Can an interface inherit multiple interfaces in Java?

Yes, we can do it. An interface can extend multiple interfaces in Java.


2 Answers

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

like image 65
BalusC Avatar answered Sep 29 '22 01:09

BalusC


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

like image 40
Jack Gajanan Avatar answered Sep 29 '22 01:09

Jack Gajanan