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.
OVERLOADING Java, along with several programming languages,allows you to reuse a method name for more then one method . In some circumstances, you might want to write several methods in the same class that do basically the same job with different arguments. When you write code to call one methods, the appropriate one is chosen according to the type of argument or arguments that you supply. Two rules apply to overloaded methods:
OVERRIDING In the class hierarchy, when a methods in a sub class has the same name and type signature as method in the superclass, then the method in the subclass is said to override the method in superclass. Methods in the derived class can have same name as that of the base class. If you are having one base class called book and to derived class called book1 and book2 and if you use same methods in all the three classes, then the last derived class method is executed although there are methods with similar names in all the former classes.The concept in Java is called as Overriding.
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 callsparseModel(element)
they will still end up polymorphically in your implementation. Compare that with overloading (e.g. by adding another parameter) where the original implementation inModelParser
would be called.Read more: https://javarevisited.blogspot.com/2011/12/method-overloading-vs-method-overriding.html#ixzz3TbRYYM5z