In Java I have an abstract base class, let's say WrapX
, which contains a property of a type, say X
(think Decorator DP). This class presents a method to call a specific method on the encapsulated X
:
public abstract class WrapX{
protected X instance;
public WrapX(X x){
this.instance = x;
}
public void foo(){
instance.foo();
}
}
public class X {
public void foo(){
System.out.println("foo");
}
}
There is then a class called Y
that extends from X
and provides an additional method:
public class Y extends X {
public void bar(){
System.out.println("bar");
}
}
Then naturally I have created WrapY
that can be used as a decorated type over the type Y
:
public class WrapY extends WrapX{
...
public void bar(){
instance.bar();
}
}
So herein lies the issue. WrapY
has inherited the instance
property of type X
from its parent WrapX
. As far as Eclipse is concerned, instance
is of type X
and so will complain that it contains no method .bar()
.
Fair enough of course, but how then in this subclass can we implicitly cast the instance
to an instance
of Y
(a valid subclass of the initial type X
)... WITHOUT the need for explicit cast ascriptions littering the code, or variable shadowing?
If I just had this in the constructor:
public WrapY(Y y){
this.instance = y;
}
Eclipse still complains that .bar()
is not a method of type X
because I guess it cannot infer for certain that WrapY(Y y)
will be used prior to construct the WrapY
instance:
public void bar(){
instance.bar(); // ERROR
}
Here is the current approach I have, littered with casts:
public WrapY(Y y){
(Y)instance = y;
}
public void bar(){
((Y)instance).bar();
}
I haven't come across this particular type of architectural problem in my experience before, file it under 'Decorator-Based-Inheritance-Type-Casting'(!)... Please enlighten me as to how I can model this in a better way.
Another issue is that, if in future someone extends WrapY
, the type of instance their class inherits will be the natural (uncasted) type of X
, when they may reasonably assume it should be of type Y
.
Thanks