What I want to do
I have an interface called strategy
that has one method strategise
. This method takes an interface called Entity
as a parameter.
public interface Strategy{
void strategise(Entity entity);
}
public interface Entity {
void do_something();
}
I have a class called EntityImpl
that implements Entity
. EntityImpl
has an additional methods that Entity
does not.
public class EntityImpl implements Entity{
void do_something() // defined in Entity interface
void do_something_else() // unique to Person
}
I have another class StrategyImpl
that implements strategy
.
StrategyImpl
expects aEntityImpl
object to be passed as the parameter to the function it implements fromStrategy
:strategise
.- This is because
StrategyImpl.strategise()
calls the method unique toEntityImpl
which is not defined inEntity
:do_something_else()
.
public class StrategyImpl implements Strategy {
void strategise(Entity entity){
entity.do_something_else();
}
}
What I have tried to do
- If I use the code above, it won't work as
entity.do_something_else()
can't be found asdo_something_else
is unique toEntityImpl
and not defined inEntity
.
public class StrategyImpl Strategy{
void strategise(Entity entity){
entity.do_something_else();
}
}
- I've tried using a
EntityImpl
object instead of anEntity
object as the parameter definition inStrategyImpl.strategise()
but this doesn't work as it thinks I'm not implementingEntity.strategise()
public class StrategyImpl Strategy{
void strategise(EntityImpl entity){
EntityImpl.do_something_else();
}
}
- I've tried using generics and wildcards, but I'm not familiar with java.
public interface Strategy {
void strategise((E implements Entity) entity);
}
Any help would be very appreciated
Thanks for your time!