transferred from: http://blog.csdn.net/cdsnmdl/article/details/3968688
————————————————————————————————————
1. Method Inheritance
:Use the Extends keyword one method to inherit another method, and inherit only one class directly.
- When the sub class and the base class are in the same package, the Sub class inherits the public/protected/default level of variable methods in the base class
- Inherit public/protected-level variables and methods at different packages.
2. Method overloading
:If the method name of two methods is the same, but the parameters are inconsistent, then one method can be said to be overloaded with another method.
- Method name is the same
- The parameter type of the method, with at least one difference in order of number
- The return type of the method can be different
- The modifiers of the method can be different
- The main method can also be overloaded
3. Method Coverage: If you define a method in a subclass whose name, return type, and parameter signature match exactly the name, return type, and parameter signature of a method in the parent class, you can say that the method of the subclass overrides the method of the parent class.
- The method name return type of the subclass and the parameter signature must be consistent with the parent class
- Subclass methods cannot narrow the access rights of a parent class method
- Subclass methods cannot throw more exceptions than the parent class method
- Method overrides exist only between subclasses and parent classes and can only be overloaded in the same class
- Static methods of a parent class cannot be overridden by a quilt class as a non-static method
- A subclass can be defined in a static method with the same name as a static method of the parent class to hide the static method of the parent class (which satisfies the overwrite constraint) in the subclass.
- And the Java virtual machine binds the static method to its owning class, and binds the instance method to the instance it belongs to.
- A non-static method of a parent class cannot overwrite a quilt class as a static method
- Private methods of the parent class cannot be overridden by the quilt class
- The abstract method of the parent class can be overridden in two ways by the quilt class (i.e. implementation and overwrite) (P169)
- Non-abstract methods of the parent class can be overridden as abstract methods
4. Super Keyword:Both the super and the This keyword can be used to override the default scope of the Java language, making the masked method or variable visible (invisible P171 in three cases).
- member variables and methods for the parent class use Super Access compilation error for private
- In the construction method of the class, the constructor of the class's parent class is called through the super statement
- To access the masked methods and properties of the parent class in a subclass of class
- The Super keyword can only be used within a constructor or instance method, and super is not available in static and static code blocks
5, polymorphic:
- For a variable of a reference type, the Java compiler processes it according to its declared type
- For a variable of a reference type, the runtime Java Virtual machine processes the object that it actually refers to
- In the runtime environment, when you access the methods and properties of the referenced object by referencing the type variable, the Java virtual machine takes the following binding rules
1) The method binding of the instance method to the object that the reference variable actually refers to, which is a dynamic binding 2) a method binding of a static method to a type declared by a reference variable, a static binding 3) a member variable (including static and instance variables) that is bound to a member variable of the type declared by the reference variable, belongs to the static State bindings
6, the advantages and disadvantages of inheritance and the use of principles:
- The number of integrations cannot be too many levels
- The upper level of the integration number is the abstraction layer
(1) The same properties and methods are defined for the lower subclass, and implemented as default as possible, thus improving reusability (2) Represents the interface of the system and describes the services that the system can provide
- The biggest weakness of inheritance: breaking packages
- Specially designed for inherited classes
(1) These classes must be provided with good documentation (2) to encapsulate the implementation details of the parent class as much as possible, defining the attributes and methods representing time details as private types (3) If some implementation details must be accessed by the quilt class, defined as the protected type (4) Define methods that do not allow subclass overrides to be final type (5) The construction method of the parent class does not allow the invocation of a method that can be overridden by a quilt class (6) If some classes are not designed for inheritance, it is unsafe to inherit it arbitrarily.