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.
public interface IXyz { }
public class XyzImpl implements IXyz {
public void doSomething() { ... }
}
public interface IAbc {
public void setXyz(IXyz val);
public IXyz getXyz();
}
Implementation 1:
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();
}
}
Implementation 2:
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();
}
}
Basically I only want to use my implementation XyzImpl within my implementation AbcImpl.
In any case, if I access the method abc() of one of my implementations, the accessing method needs to cast as well.
public void method() {
AbcImpl1 abc;
XyzImpl xyz = (XyzImpl) var.getXyz();
xyz.abc();
}
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?
Thank you in advance for your feedback!
BR, bobbel