Java: Overriding or Overloading method?

2019-04-30 02:30发布

问题:

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.

回答1:

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.



回答2:

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:

  1. The return type of the methods can be different, but the argument lists of overloaded methods must differ.
  2. The arguments lists of the calling statement must differ enough to allow unambiguous determination of the proper method to call.

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.



回答3:

1) First and most important difference between method overloading and overriding is that, In case of method overloading in Java, signature of method changes while in case of method overriding it remain same.

2) Second major difference between method overloading vs overriding in Java is that You can overload method in one class but overriding can only be done on subclass.

3) You can not override static, final and private method in Java but you can overload static, final or private method in Java.

4) Overloaded method in Java is bonded by static binding and overridden methods are subject to dynamic binding.

5) Private and final method can also be not overridden in Java.

Read more: https://javarevisited.blogspot.com/2011/12/method-overloading-vs-method-overriding.html#ixzz3TbRYYM5z