Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Overriding or Overloading method?

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.

like image 637
Michael Avatar asked Jun 29 '11 15:06

Michael


1 Answers

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.

like image 132
Jon Skeet Avatar answered Sep 29 '22 19:09

Jon Skeet