java- rewrite, heavy

  1. Overload (Overload)
    1. Method overloading is to let the class in a uniform way to handle different types of data as a means. A plurality of functions with the same name exist, having different number of parameters / types.
    2. Overloading is a polymorphism of a performance.

      Java method overloading, is in a class can create multiple methods , they have the same name but with different parameters and different definitions .

      Decide when to call the method passed to them by the number of different parameters and parameter types what methods specific use, this is polymorphism.

       When overloaded, the method name to be the same, but not the same number and type of parameters, return type may be the same or different. Unable to return typed overloaded function as a distinguishing criterion.

      The method of the parent class is modified by default, only in the same package, which is a subclass is overridden if the package is not in the same can not be rewritten.

      The method of the parent class when protoeted, not only in the same package, which is a subclass is rewritten, the packet can also be different subclasses override.

    3. rule:

      1), must have a different list of parameters;

      2), you can have different types of return, a different parameter list as long as it;

      3), you can have different access modifiers;

      4), can throw different exception;

  2. Rewrite (Override)

           If the child class and create a method, the same type of return value of a parent class with the same name, the same list of parameters , but different implementations body, to realize the function is different from the parent class, method is called in this way rewrite, also known methods covered.

          rule:

1), the parameter list must be exactly the same as the method is overridden, otherwise it could not be called to rewrite but overloading.

2), the return type must always return the same type of method is overridden, but otherwise could not be called to rewrite overloaded.

3), to limit the access modifier must be greater than the access modifier is rewritten method ( public> protected> default> Private )

4), the overridden method must not throw new abnormalities or abnormal ratio is overridden method stated broader examination type. E.g:

A parent class method declared a checked exception IOException, rewriting this method is you can not throw Exception, IOException subclass can only throw an exception, you can throw an unchecked exception.

Features:

1. Override Features

  1, a method of covering the mark must be exact match and mark method is covered in order to achieve the effect of coverage;

  2, the return value of the method must return a consistent coverage and methods covered;

  3 must be consistent abnormality abnormality, the method of covering thrown and thrown covered method, or is a subclass;

  4, the method can not be covered by the private, or subclasses thereof defines a new method only, and not be overridden.

2.Overload Features

  1, in the use of heavy-duty only through different parameter style. For example, different types of parameters, number of different parameters, a different sequence parameter (of course, several types of parameters within the same method must be different, for example, may be fun (int, float), but not as fun (int, int ));

  2, can not access, return type, exceptions thrown overloaded;

  3, exception type and number of methods will not affect overloaded;

  4, inheritance, if the access is a method in the parent class is priavte, then it can not be overloaded in a subclass, if defined, only defines a new method, without reaching the weight load effect.

to sum up:

override (rewrite)

   1, method name, arguments, return the same value.

   2, subclass method can not be reduced access to the parent class method.

   3, subclass method can not throw more exceptions than the parent class method (methods subclass but may not throw an exception).

   4 exists between the parent and child classes.

   5, is defined as a final method can not be rewritten.

 overload (overloading)

  1, parameter types, the number, at least one sequence are not the same. 

  2, can not override the return value only different method name.

  3, present in the parent class and subclass of the same.

           

Guess you like

Origin www.cnblogs.com/zimo-bwl1029-s/p/11272321.html