Current location - Quotes Website - Signature design - What's the difference between polymorphism and overloading in JAVA?
What's the difference between polymorphism and overloading in JAVA?
Polymorphism

Through inheritance, a class can be used as many types: when implementing an interface, it can be used as its own type, any base type or any interface type. This is called polymorphism.

Overloaded

Each type member has a unique signature. Method signature consists of method name and parameter list (order and type of method parameters). As long as the signatures are different, you can define multiple methods with the same name in a type. When two or more methods with the same name are defined, it is called overload. That is, when overloaded, the parameter list of members with the same name is different (parameter order and type).

Inherit, Override and Hide Members

Derived types inherit all members of their base types; That is, these members are defined above and can be used to derive types. You can modify the behavior and quality of inherited members in two ways:

1. Derived types can hide inherited members by defining new members with the same signature. This method can be used when changing a former public member to a private member or defining a new behavior for an inheritance method marked as final.

2. Derived types can override inherited virtual methods. Overriding a method provides a new definition of a method that will be called at run time according to the type of the value, not according to the type of the variable known at compile time. Members can override a virtual method only if it is not marked as final and the new method can be accessed at least like a virtual method.

The method name and parameters are the same as form rewriting. The rewritten method cannot reduce the visibility of the original method, nor can it change the return value type of the original method.

With the same method name and different parameters (number and type), the overloaded method can be regarded as a brand-new method, which has different "visibility" and "return value type" compared with the original method. For example:

Level a

protected int method 1(int a,int b){ return 0; }

}

Public class b extends A{

public int method 1(int a,int b){ return 0; }//Correct, overriding the parent class method can extend the access rights.

//private int method 1(int a,int b){ return 0; }//Error, overriding the parent class method cannot reduce the access rights.

//private long method 1(int a,int b){ return 0; }//Error, override the parent class method, and cannot change the return value type.

public short method 1(int a,long b){ return 0; }//Correct. A method that overloads itself can have different access rights and return value types.

private int method 1(int a,long b){ return 0; }//Correct. A method that overloads itself can have different access rights and return value types.

}

But the methods here share the short method 1 (int a, long b){ return 0;; } and method private int method 1 (int a, long b){ return 0;; } cannot exist at the same time, because methods with the same name and parameter type (overriding methods) are not allowed in the same class.