0

Say I have an interface A and a class B that implements it. Now, I also have some class C which extends class D (which means that it can't also extends B) but I also need there the functionality of interface A.

The solution I know is to have a member of A instantiated by B in C (which will implement A) and when implementing the functions of A call the matching function from the member of A.

Is there any way to create some connection between the functions of A and the member inside C? (so that java will know that every time it needs to call a function from A it will directly go and and run the matching function from the A member without me needing to write the code for it for every function of A)

A big thank you is waiting to each one of the helpers...

user1034092
  • 67
  • 1
  • 5
  • Design problems shouldn't be approached in this way. Show us how A,B C and D are related. – TheLostMind Sep 10 '14 at 07:28
  • If you are bothered only by the manual labor of writing the forwarding calls, your IDE (assuming that you use one) will probably have functionality to help you. – Jae Heon Lee Sep 10 '14 at 07:30
  • 3
    _"The solution I know is to have a member of B in C"_. You should have a member of type A in C so that C only depends on the interface A and not the implementation B. – Benjamin Gale Sep 10 '14 at 07:32
  • @JaeHeonLee I'm looking for this option so that if I'll need to add some functionality to A in the future - I won't need to change the code of C.... – user1034092 Sep 10 '14 at 07:34
  • @TheLostMind Well, the problem is like this. I have two classes X and Y that extends X. Those are "of the shelf" classes so I can't touch them. I've created a class Z that extends X. what I'm looking for is a way to create a new class K that will extends Y but will also get each new function I'll add to Y in the future (without the need to make changes to the code of that new class K). – user1034092 Sep 10 '14 at 07:42

2 Answers2

2

No. As already stated delegation must be implemented manually.

Having said that, you have a few options to simplify this: If you're working with Eclipse, select Source|Generate Delegate Methods... and select your member variable. Eclipse will then generate all the delegate methods for you. I don't know about other IDEs, but I would be surprised, if NetBeans et al. would not have a similar feature.

Another option, if you actually want to decorate existing collection classes, consider Google Guava's Google Guava's Collection Helpers.

Last, but not least, you could consider restructing your code and decorate your classes using Advices. Advices stem from Aspect Oriented Programming (AOP) and typically use a proxying mechanism to enrich original target classes. This is a rather advanced technique, but if you are determined to go down this road, have a look at Spring's AOP support.

Stefan Haberl
  • 9,812
  • 7
  • 72
  • 81
0

So to sum up, here is your class hierarchies:

package common;
public interface A 
{
  void doStuff();
}
package commom.impl;
public class B implements A 
{
  void doStuff() {}
}
package real.service;
public class D 
{
  void doSomeRealStuff() {}
}
package real.service;
public class C extends D 
{
  void doSomeRealStuffForGood() {}
}

Assuming that each class is declared in its own source file.

Just to recall from the OP, I assume you need B stuff in C and not really A stuff. Because A is nothing but a contract and you need then the real implemting class to be fetched inside your C class in order to call the declared methods on.

In such a case, you may need to use the Inversion of Responsability approach, so that you declare an instacne of type B inside your C clas then you layer each method from B with a one having the same signature and that do nothing but delegate the real call to the instance member:

package real.service;

import common.A;
import common.impl.B;

public class C extends D 
{
  private A delegate;

  public C ()
  {
    delegate = new B();
  }

  void doStuff() {
    delegate.doStuff(); // Call the real delegate method when doStuff is called on an isntance of C.
  }

  void doSomeRealStuffForGood() {}
}

Note that this is a legal OO concept, since you are following an HAS-a even though some could consider it a high coupling.

Otherwise if you are not tied to the B class, and you may drop the declare methods in there for some others, you can declare an inner class that implements the A interface the way you need.

Edit:

Java does not support multiple inheritance, though you have provided a common contract in your A interface, so if you need all those methods (behavior) to be availble in your C class, it would be better to implement it directely and override all the interface methods.

tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • First of all - Thanks! That was the implementation I was talking about. The "problem" with this implementation is that each time I add a new function to A (and implement it in B) I also need to go to C and add the the delegation for that new function, and that is the thing I was trying to avoid... It seems that I don't really have a choice... :-) – user1034092 Sep 10 '14 at 08:24
  • Why not then implement the `A` interface from `C`? Look at my updated answer. – tmarwen Sep 10 '14 at 11:22