The difference between rewriting, overloading, and overriding in java

Polymorphism 
  Through inheritance, a class can be used as multiple types: as its own type, as any base type, or as any interface type when implementing an interface. polymorphism 

Overloading 
  each type member has a unique signature. A method signature consists of the method name and a parameter list (the order and types of the method's parameters). Multiple methods with the same name can be defined within a type as long as the signatures are different. When two or more methods with the same name are defined, it is called overloading. That is, the parameter lists for members of the same name are not the same (parameter order and types) when overloading. 

Inheriting, Overriding and Hidden Members (override = override)
  A derived type inherits all members of its base type; that is, those members are defined on top of the derived type and are available to the derived type. The behavior and quality of an inherited member can be modified in two ways: 

  1. A derived type can hide the inherited member by defining a new member with the same signature. This approach can be taken when making a previously public member private, or when defining new behavior for an inherited method marked final. 

  2. Derived types can override inherited virtual methods. Overriding a method provides a new definition of the method that will be called based on the type of the value at run time, rather than the variable type known at compile time. A member can override a virtual method only if the virtual method is not marked final and the new method is at least as accessible as the virtual method. 
The method name and parameters are the same to form an overriding. The overridden method cannot reduce the "visibility" of the original method, nor can it change the return value type of the original method. 

  The method name is the same, and the parameters are different (number, type) to form overloading. The overloaded method can be regarded as a brand new method. Compared with the original method, it can have different "visibility" and "return value type". For example: 

class A { 
protected int method1(int a, int b) { return 0; } 

public class B extends A{ 
public int method1(int a, int b) { return 0; } //correct, override the parent class method to expand access rights 
//private int method1(int a, int b) { return 0; } //error, Overriding the parent class method cannot reduce access rights 
//private long method1(int a, int b) { return 0; } //Error, override the parent class method, cannot change the return value type 
public short method1(int a, long b) { return 0; }//Correct, overload its own method, can have different access rights and return value types 
private int method1(int a, long b) { return 0; }//Correct, overload itself methods, can have different access rights and return value types 


  but here method public short method1(int a, long b) { return 0; } and method private int method1(int a, long b) { return 0; } cannot Both exist because methods (overridden methods) with the same name and parameter types are not allowed in the same class.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325337880&siteId=291194637