Inheritance in Java is one of the three main features of Java object-oriented, which is still a lot of knowledge points. Now make the following summary:
1. Concept of inheritance:
Inheritance is special in its own--general relationship, that is, often said is-a relationship. The subclass inherits the parent class, indicating that the subclass is a special parent class and has a
Some properties or methods.
2. Initialization sequence in inheritance:
From the structure of a class, there are four common patterns within it: attributes (including class properties and instance Properties), methods (including class methods and instance methods), constructors, and
Initializes the block (including the initialization block of the class and the initialization block of the instance). For the initialization order in the inheritance, it also divides into the initialization of the class and the initialization of the object.
Class initialization:
During the preparation phase of the JVM load class, the memory space is first allocated for all class properties and class initialization blocks of the class. And initializes it during the first initialization phase of the class, between class properties and class initialization blocks
The order in which they are initialized determines the order in which they are defined. If the class has a parent class, the class properties and class initialization blocks of the parent class are initialized first, beginning with the object class.
Object initialization:
When new creates an object, it first allocates memory for object properties and initialization blocks, and performs default initialization. If there is a parent class, first allocate memory and perform initialization for the parent class object and initialization block.
The initializer in the parent class constructor is then executed before the object properties and initialization blocks of the child class are initialized.
Note:
1. During the object initialization phase, both properties and methods are for properties and methods that subclasses can inherit from the parent class, generally in the case of non-private to the parent class.
Because the private adornment is unique to the parent class, the subclass does not inherit, and when the new subclass does not need to allocate space for it and perform initialization. Of course, the constructor subclass of the parent class is not inherited.
But the constructor is another matter.
2. Class initialization is performed only once, and when multiple objects are new to the same class, class properties and class initialization blocks are initialized only once.
3. Shadowing in Inheritance:
Hidden meaning: actually exists, but is not visible to the outside.
The Java class has three access controls: private, protected, and public, and is represented as a default access control state when the three access control characters are not written. Therefore, there are four levels of access control altogether.
The specific access control performance is as follows:
The property or method of private modification is unique to the class and cannot be accessed directly in any other class;
The property or method of the default adornment has a package access attribute, and other classes in the same package can be accessed;
Protected-Modified properties or methods can be accessed in other classes in the same class, and also in subclasses that are not in the same package;
Public-decorated properties or methods can be accessed directly in the outer class.
When a subclass inherits the parent class, the subclass can inherit the properties and methods (generally non-private adornments) in the parent class that have access control permissions, and the subclass does not inherit from the properties and methods that are unique to the private decorated parent class.
When a subclass needs to change the inherited method, it is often said to override the parent class. Once overridden, this method of the parent class behaves as hidden for the subclass. After the object of the subclass calls this method, it is called after the subclass is overridden
method, but there are two ways in which you can invoke the original method of the parent class in the subclass object:
1. Force the Subclass object type to be converted to the parent class type and make the call;
2. Call through Super.
Similarly, if a property of the same name in the parent class is defined in a subclass, the parent class property is represented as hidden in the subclass.
4. This and super in succession:
The This in the constructor represents the object reference that is currently being initialized, and this in the method represents the object reference that is currently calling this method. This specific usage is shown in a few ways:
1. When there are multiple overloaded constructors, and one constructor needs to call another to construct it, it is called with this (Param) in its first row and only in the first row;
2. When a method in the object needs to call the other methods in this object, use this as the keynote, or do not write, in fact, the default is this as the keynote;
3. When the object property and the local variable name in the method are the same, in this method you need to explicitly use this as the keynote to represent the properties of the object, if this problem does not exist, you can not explicitly write this.
In fact, one of the problems involved is the search rule for variables: first local variables, and the variables defined in the current class, the variables that can be inherited by the quilt class defined in the parent class, and the parent class ...
Super represents calling the corresponding properties and methods in the parent class. In the method, if you need to call the method of the parent class, be sure to write it in the first line
5. Inheritance and combination:
From the simple realization effect, the inheritance and the combination can achieve the same goal. and are an effective way to implement code reuse.
But in the general concept level, the two have the obvious difference.
Inheritance manifests itself as a general--special relationship, a subclass is a special parent class and a is-a relationship. The parent class has a generic attribute for all subclasses.
The combination manifests as the whole--part relation, namely has-a relation. In the composition, the "part" is extracted separately to form its own class definition, and in the "whole"
In this class definition, a section is defined as one of these properties, and through the Get and set methods, you can invoke the properties and methods in the "partial" class.
Original Address http://www.cnblogs.com/lwbqqyumidi/p/3509992.html
Java Inheritance Knowledge Summary