D
Daniel
I think my question is best posed by the code:
class Base<Q extends Quantity>{
private int _val = 0;
public Base(int val){
_val = val;
}
public Base<Q> plus(Base<Q> b){
return new Base<Q>(_val + b._val);
}
}
class Derived1 extends Base<A> { //class A extends Quantity
public Derived1(int val){ super(val);}
}
(I don't believe the generics are relative to my question, but I'm
using them to keep in parallel with my real-world problem.)
I want to be able to do something like the following,
...
Derived1 billy = new Derived1(5);
Derived1 bob = new Derived1(6);
Derived1 billyPlusBob = (Derived1)billy.plus(bob); //
billyPlusBob._val is 11 (if you could access it)
...
I understand this is nonsense because the billy.plus call is returning
a reference of type Base<A> which is obviously not type Derived1,
which is why I would get a ClassCastException when trying to cast the
result of billy.plus(bob) to Derived1.
What's stumping me is why I can't figure out how to accomplish the
seemingly trivial task of putting the 'plus' logic in the base class
so that I don't have to implement it in each derived class. What's
throwing me off is that the plus method has to return a reference of
the _derived_ type, which is seemingly impossible if I'm implementing
the method in the base class. I'd really, really like to avoid having
to push down the plus method (b/c in reality there are many more
'plus'-like methods and many, many derived classes all of whose logic
is the same), but I absolutely need it to return a reference of the
derived type (or at least one that could be cast to the derived type).
Anybody ever had the same problem? Any suggestions/insights?
class Base<Q extends Quantity>{
private int _val = 0;
public Base(int val){
_val = val;
}
public Base<Q> plus(Base<Q> b){
return new Base<Q>(_val + b._val);
}
}
class Derived1 extends Base<A> { //class A extends Quantity
public Derived1(int val){ super(val);}
}
(I don't believe the generics are relative to my question, but I'm
using them to keep in parallel with my real-world problem.)
I want to be able to do something like the following,
...
Derived1 billy = new Derived1(5);
Derived1 bob = new Derived1(6);
Derived1 billyPlusBob = (Derived1)billy.plus(bob); //
billyPlusBob._val is 11 (if you could access it)
...
I understand this is nonsense because the billy.plus call is returning
a reference of type Base<A> which is obviously not type Derived1,
which is why I would get a ClassCastException when trying to cast the
result of billy.plus(bob) to Derived1.
What's stumping me is why I can't figure out how to accomplish the
seemingly trivial task of putting the 'plus' logic in the base class
so that I don't have to implement it in each derived class. What's
throwing me off is that the plus method has to return a reference of
the _derived_ type, which is seemingly impossible if I'm implementing
the method in the base class. I'd really, really like to avoid having
to push down the plus method (b/c in reality there are many more
'plus'-like methods and many, many derived classes all of whose logic
is the same), but I absolutely need it to return a reference of the
derived type (or at least one that could be cast to the derived type).
Anybody ever had the same problem? Any suggestions/insights?