Summary of learning Java method overloading and method overrides

In the phase I study learning method overloading ( Overload ), and in the time to learn the characteristics of the object-oriented inheritance of the three I learned how to rewrite ( Override ).

 

concept:

Method overloading: In the same class, there is a method to allow more than one of the same name, as long as they are different or the number of parameters can be of different types.

Method overrides:  subclasses and methods of the parent class appears in exactly the same declaration, method name and parameter list are the same.

 

Features:

Method overloading: nothing to do with the return value type, just look at the method name and argument list when calling the virtual machine to distinguish between the different methods of the same name parameter list.

Method overrides :  method name to call the same sub-class when creating a subclass object.

 

 

scenes to be used:

Method overloading: the need to define two or more methods of the same class, functionally similar method but not the same type of parameters, to make use of the same method name overloading.

Method overrides: When required function subclass the parent class, subclass and main features have their own unique features, the parent class can be rewritten. So that is a function of the parent class followed, but also defines the type of unique features (using the super keyword)

 

 

Precautions:

Method overloading:

  The method of overload parameter list can be divided into: 1 , number of different parameters;  2 , different parameter types,  three different types of multi-order of the parameters.

  举例: 1 ( int a, int b) (int a, int b, int c)

     2、  (int a, int b)      (double a, doule b) 

     3、  (int a, doule b)     (doulbe a, int b)

  Unrelated factors: 1, 2 has nothing to do with the parameter name, regardless of the type of the return value

Method overrides:

  1, private methods of the parent class subclasses can override.

  2, Subclasses override the parent class method access not lower ( the Java in a total of four access control, the access control is such that the size of the case: public> protected>  ( default )   > Private  ).

Permissions modifier

 

public

protected

default

private

The same class

 √

 √

 √

Buns same class , other classes

 √

 √

 √

 

Different classes buns

 √

 √

 

 

Other classes in different packages

 √

 

 

 

  3, sub-class method returns the value must be less than equal to the parent class method returns the value range.

  4, static methods are also used to write a static manner.

 

 

Guess you like

Origin www.cnblogs.com/ryanskc/p/11834644.html