title: Java: Implementation of Interface with Methods passing Interface parameter
body: <p>I have a given interface called IAbc, which consists of a method with a parameter of interface IXyz. I am not sure whether I should create an attribute IXyz or an attribute XyzImpl in my implementation of IAbc.</p>
  
  <pre><code>public interface IXyz { }
  public class XyzImpl implements IXyz {
     public void doSomething() { ... }
  }
  
  public interface IAbc {
     public void setXyz(IXyz val);
     public IXyz getXyz();
  }
  </code></pre>
  
  <p>Implementation 1:</p>
  
  <pre><code>public class AbcImpl1 implements IAbc {
     private XyzImpl attr;
     public void setXyz(IXyz val) {
        attr = (XyzImpl) val;
     }
     public IXyz getXyz() {
        return (IXyz) attr;
     }
     public void abc() {
        attr.doSomething();
     }
  }
  </code></pre>
  
  <p>Implementation 2:</p>
  
  <pre><code>public class AbcImpl2 implements IAbc {
     private IXyz attr;
     public void setXyz(IXyz val) {
        attr = val;
     }       
     public IXyz getXyz() {
        return attr;
     }
     public void abc() {
        ((XyzImpl)attr).doSomething();
     }
  }
  </code></pre>
  
  <p>Basically I only want to use my implementation XyzImpl within my implementation AbcImpl.</p>
  
  <p>In any case, if I access the method abc() of one of my implementations, the accessing method needs to cast as well.</p>
  
  <pre><code>public void method() {
     AbcImpl1 abc;
     XyzImpl xyz = (XyzImpl) var.getXyz();
     xyz.abc();
  }
  </code></pre>
  
  <p>Which way of implementation makes more sense? Casting in all classes where my implementation will be used is also not very handy. Is there any way to use only my implementation classes and not the interfaces?</p>
  
  <p>Thank you in advance for your feedback!</p>
  
  <p>BR, bobbel</p>