Rewrite and heavy-duty (excerpt notes)

Rewrite (Override)

Rewriting implementation is a subclass of method allow access to the parent class is re-written, the return value, and can not change parameter.

  • Benefits rewritten: sub-class according to the needs, define your own unique (that is, a subclass can implement the parent class method in accordance with demand)

Method of rewriting rules:

  • Exactly the same argument list must be rewritten methods.

  • The return type is overwritten with the return type may not be the same, but must return value is the parent class of the derived class

  • Access can not be lower than the parent class is rewritten access method. For example: If a parent class is declared as public, so override this method in a subclass can not be declared as protected.

  • Members of the parent class method can only be rewritten its subclasses.

  • Declared final method can not be overridden.

  • Declared as static methods can not be rewritten, but can be declared again.

  • The parent class and subclass in the same package, then the subclass can override the parent of all methods, except for the private method declaration and final.

  • Sub-classes and superclasses are not the same package, then the subclass can only override the parent class declared as non-final public or protected methods are.

  • The method of construction can not be rewritten.

  • If you can not inherit a method, you can not override this method.

Overload (Overload)

In one class which is overloaded, method of the same name, but different parameters . Return type may be the same or different.

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

Overloading rules

  • Overloaded method must be changed parameter list (number of parameters or not the same type);

  • Overloaded method may change the return type;

  • The method may be varied overloaded access modifiers;

Rewrite and reload the difference

 

 

(1) a method overload is defined in the same class plurality method name, the number of different parameters or their type and sequence of the same number but different

(2) the name of the method is a method to rewrite the parent class in the same sub-class there is a method, and the same number and type of parameters, the method returns the same value

(3) a method overload class exhibit polymorphism, and the polymorphism is a method of rewriting performance of the child and parent class.

 

 

 

Guess you like

Origin www.cnblogs.com/bzbz/p/11493719.html