I have a method, in a class called "PlaceParser" that extends
"ModelParser":
protected Place parseModel(JSONObject element) ...
A Place is a sub class of Model.
Should the @Override
annotation be added to the above code? As the method has a different return type, does this still count as overriding the base class method with the same name and arguments / does the return type alter the 'signature'?
The "ModelParser" method looks like this "ModelT" also extends "Model":
protected abstract ModelT parseModel(JSONObject element)
throws JSONException;
Update @Jon Skeet:
The base class is declared like this:
public abstract class ModelParser<ModelT extends Model> {
I hadn't seen a <ModelT extends Model>
style declaration for a class before.
Yes, you should add @Override
, as you're still overriding the method. The fact that you're using covariance of return types doesn't change that.
In particular, if other code has an expression of type ModelParser
and calls parseModel(element)
they will still end up polymorphically in your implementation. Compare that with overloading (e.g. by adding another parameter) where the original implementation in ModelParser
would be called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With