Can anyone help me to fix the code in the Main so that is works as intended?
Gurus will know what I'm after.
TIA
Please see below:
public abstract class MyBaseElement {...} public class MyElementA extends MyBaseElement {...} public class MyElementB extends MyBaseElement {...} public class MyElementC extends MyBaseElement {...} public interface Mapper<T> {...} public class MyMapperA implements MyMapper<MyElementA> {...} public class MyMapperB implements MyMapper<MyElementB> {...} public class MyMapperC implements MyMapper<MyElementC> {...}
// this method returns an instance of
// one of the child classes: MyElementA, MyElementB, MyElementC, etc...
public MyBaseElement getSpecificElement() {...}
// this method returns a specific mapper based on the element
public Mapper<? extends MyBaseElement> getSpecificMapper(MyBaseElement element) { if (element instanceof MyElementA) return new MyMapperA; if (element instanceof MyElementB) return new MyMapperB; if (element instanceof MyElementC) return new MyMapperC; ... }
// Main ... MyBaseElement element = getSpecificElement(); // the line below compile Class<? extends MyBaseElement> myElement = element ; //the line below does not compile: can not convert from MyBaseElement to Mapper<? extends MyBaseElement> // Any ideas, please? Mapper<? extends MyBaseElement> myMapper = getSpecificMapper(myElement ) myMapper.doStuff(); ... // end-Main