How should I declare an instance of an inner class of classes that implement the same interface

I have the following structure

public interface I{...}

public class A implements I{
  ....

  public static class Inside{
    ...
    //data from here are used in A. Therefore tight coupling.
  }
}

public class B implements I{
  ....

  public static class Inside{
    ...
    //data from here are used in B.  Therefore tight coupling.
  }
}

Note that the implementation of A.Inside is somewhat different from B.Inside. Then I have another class that accesses Inside as

public class AW extends X{
  ...
}

public class BW extends X{
  ...
}

public class X{
  ...
  public Obj doSomeWork(Inside inside){
    mInside = inside;
    ....//some work
  }
}


//UsageB is similar in it's implementation of the B stack
public class UsageA{
  A.AW inst = new AW();
  Inside in = A.Inside();
  inst.doSomeWork(in);
}

question: What should be the type of mInside? I don't want to have two mInside variables.

AW is tailored for A and BW is tailored for B

Jon Skeet
people
quotationmark

The problem is that your two Inside types are currently unrelated - there's no way for X.doSomeWork() to do refer to "it's this type or that type" when they're entirely different types. The fact that they're both nested classes called Inside doesn't make any difference.

If you extract the common behaviour (that X wants to call) into an interface and make both Inside classes implement that interface, then you can use that commonality. Basically do exactly what you'd do if they weren't nested classes and didn't have the same name, because X doesn't care about either of those aspects.

people

See more on this question at Stackoverflow