The difference between Java overrides and overloads

  • 2020-05-30 20:07:00
  • OfStack

Rewrite (Override)

Overrides are when a subclass rewrites the implementation of a parent class's accessible methods without changing the return value or parameter. That is, the shell remains the same, the core is rewritten!

The benefit of the override is that the subclass can define its own behavior as needed. That is, the subclass can implement the methods of the parent class as needed.

The override method cannot throw a new check exception or an exception that is more general than the one declared by the override method. For example, one of the parent class's methods declares a check exception IOException, but you cannot throw an Exception exception when you override this method, because Exception is the parent of IOException and can only throw an IOException subclass exception.

Method override rules

The parameter list must be exactly the same as the method being overridden; The return type must be exactly the same as that of the method being overridden; Access rights cannot be lower than those of the overridden method in the parent class. For example, if a method of a parent class is declared public, overriding the method in a subclass cannot declare it protected. A member method of a parent class can only be overridden by its subclass. Methods declared as final cannot be overridden. Methods declared as static cannot be overridden, but can be redeclared. Since subclasses and superclasses are in the same package, subclasses can override all methods of the superclass, except those declared as private and final. If a subclass and its parent are not in the same package, the subclass can override only the non-final methods declared as public and protected by the parent class. The overridden method can throw any unforced exception, whether or not the overridden method throws an exception. However, the overridden method cannot throw a new mandatory exception, or a broader mandatory exception than the one declared by the overridden method, or vice versa. Constructors cannot be overridden. If you cannot inherit a method, you cannot override the method.

Overloading (Overload)

Overloading (overloading) is in a class with the same method name but different parameters. The return type can be the same or different.

Each overloaded method (or constructor) must have a unique list of parameter types that have no 2.

You can only overload constructors

Overloading rules

The overloaded method must change the parameter list (the number or type or order of parameters is different). Overloaded methods can change the return type. Overloaded methods can change access modifiers; Overloaded methods can declare new or broader check exceptions. Methods can be overloaded in the same class or in a subclass. The return value type cannot be used as a criterion to distinguish overloaded functions

区别点 重载方法 重写方法
参数列表 必须修改 1定不能修改
返回类型 可以修改 1定不能修改
异常 可以修改 可以减少或删除,1定不能抛出新的或者更广的异常
访问 可以修改 1定不能做更严格的限制(可以降低限制)


Method overwriting (Overriding) and overloading (Overloading) are different manifestations of java polymorphism. Overwriting is one manifestation of polymorphism between parent class and subclass, while overloading is one manifestation of polymorphism in class 1.

Related articles: