0

I've got a plugin architecture for a Java application I'm working with.

All the examples show:

   public class SpecialName implements BaseExtenderClass, ClassB, ClassC {
   //Stuff
   }

Where "SpecialName" is a name you have to name your class in order for it to be recognized as a plugin, and BaseExtenderClass, ClassB, ClassC are all different API functionality that are extended in that one class to do certain operations. Each one has their own methods that must be implemented.

What I'm wondering is if there is a way in Java I can put ClassB and ClassC in separate files from the BaseExtenderClass with SpecialName, and still have it behave for all intensive purposes as if the original code I listed above.

The code would be a lot neater and more organized I could do this since the plugin I'm writing will actually extend a good 6-8 of the available api interfaces.

If not, I can have one massive file... it just doesn't feel like the right way to do things.

Doug
  • 6,446
  • 9
  • 74
  • 107

2 Answers2

1

Are you trying to split the interface definitions or the implementation code for them ?

I am assuming that you are saying that you don't want to put the implementation code for all 3 interfaces in the same class but rather need to separate into 2 or more files (classes) because you commented on the potential size.

If this is the case (and I'll assume you have a good reason for doing so), you should be able to create an intermediate class that implements ClassB and ClassC, then inherit from that one.

Something like this...

public class TmpClass implements ClassB, ClassC {
  //put implementation for these 2 interfaces here
}

public class SpecialName extends TmpClass implements BaseExtenderClass {
  //add implementation for BaseExtenderClass interface here
}
TDWebDev
  • 86
  • 5
1

Your example is a bit confused. I think it should really be:

public class SpecialName extends BaseExtenderClass
   implements InterfaceB, InterfaceC {

The point is:

  • A class can extend (at most!) one other class.
  • A class can implement multiple interfaces.
  • A class cannot implement a class.

So how do you deal with the case where you have a complicated interface hierarchy AND you want to avoid duplicating the implementation code for the interface methods?

The best you can do is a combination of the following:

  • Partially mirror the interface graph as a baseclass tree, with duplication of methods where inheritance is not possible; e.g.

       public class MyClass extends BaseAB 
            implements InterfaceA, InterfaceB {
            // specific methods for MyClass
       }
    
       public class BaseAB extends BaseA 
            implements InterfaceA, InterfaceB {
            // base methods for InterfaceB
       }
    
       public class BaseA implements InterfaceA {
            // base methods for InterfaceA
       }
    
  • Implement the common functionality as components, and make your main class a wrapper; e.g.

       public class MyClass implements InterfaceA, InterfaceB {
           private InterfaceA a = new MyA(...);
           private InterfaceB b = new MyB(...);
    
           public void methodInA(...) {
               a.methodInA(...);
           };
    
           // more adapter methods, as above
    
           // specific methods for MyClass
       }
    
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216