EDIT: I will leave this here as an example. Read the comments for more information but generally: DON’T USE THIS DESIGN! It’s BAD!
I searched for an answer for a while now, but couldn’t find anything really specific saying, no you can’t because… or yes you can that’s how you do it..
So the question is, Can I create an abstract method defining Object type parameters and then have something implement it with a concrete Type of parameter like this:
public abstract class ToBeOverriden {
public Object method1 (Object parameter);
public String method2 (Object parameter);
public void method3 (Object parameter);
}
And then override it with this:
public class Implementation {
@Override
public DateTime method1 (Person parameter){
return new DateTime();
}
@Override
public String method2 (MotorCycle parameter){
return new DateTime();
}
@Override
public void method3 (String parameter){
return new DateTime();
}
}
Where Person is an object created by me. Return Type can be whatever. Currently I can’t do this. It doesn’t let me. My guess is that this is because my Class doesn’t extend Object. Although everything extends Object… So…
Or do I need to refresh my Java knowledge? 🙂
EDIT: Added a more complex class structure.
Thanks!
You would need to use Java Generics:
Added:
Eparameter can be ommited, the code will still compile. However, if different implementations of theToBeOverridenwill use different return types, I think it’s better to retainE. But that’s a matter of personal taste – I don’t like seeingObjectanywhere in code.Added 2:
As about your update in the question, you would need to have a separate Generic type for every method. For example:
However, usually, when you need such a horrible structure – then your code is designed the wrong way. In 95% cases 1 generic type parameter is enough. In 4.99% cases 2 generic type parameters are enough.