Inheritance of Android Review Road

Let me first talk about the benefits of inheritance:

1. Improve the reusability of the code

2. Let the relationship between the class and the class (the relationship is the premise of polymorphism)

By continuously extracting the content in the object, a relationship is formed, and there is inheritance, and there is a parent-child class, which improves the reusability of the code.

Inheritance features:

Inheritance in the java language can only be single inheritance

Single inheritance: A class can only have one parent class, which can be understood as a son can only have one father.

Multiple inheritance: a class can have multiple parent classes, one son has multiple fathers

Here's a simple example to explain:

Class A{

Void show(){

System.out.println("show a...");

}

}

Class B{

Void show(){

System.out.println("show b.....");

}

}

Class C extends A{} //This is a single inheritance

Class D extends A,B{

//This is multiple inheritance

D d = new D();

D.show();

}1234567891011121314151617

Let's talk about the reason why java does not support multiple inheritance: the uncertainty of the call will be generated. The reason why java does not support multiple inheritance is to say the benefits of multiple inheritance:

Inherited several classes at the same time, there will be several methods of the class

Java improved multiple inheritance, although it does not support multiple inheritance, but supports multiple inheritance, thus forming an inheritance system, as shown below

Class A extends B{}

Class B extends C{}

C

-----|B

-----|A12345

* How to use members of an inheritance system? *

1. Check the content of the top-level class of the system, because the basic functions of the inheritance system can be understood through this class.

2. Create an object of this subclass, you can get the common function, but also the function unique to the subclass. Simply put: look at the parent class content, create the object of the subclass

When is the definition of inheritance?

1. It is not allowed to define inheritance when there is a relationship between transactions.

2. Affiliation: xxxx is one of zzzz, for example: Student is a kind of human, translated in English is the relationship of is a

Myth: Don't just reuse, just inherit the content in other classes.

Inheritance in the characteristics of members:

Member variable

2. Member method

3. Construction method

Inheritance in member variables

1. The public properties contained in the parent class can be called by the subclass.

2. If the name of the attribute contained in the parent class is the same as the name of the attribute contained in the child class, and the name of the variable with the same name is also included in the method to be called.

Under normal circumstances, the method will use the principle of proximity to get the value of the variable. If you want to get the value of the property of the object of the subclass, it will be called by this. If you want to get the value of the property of the parent class, it will be called by super.

Next, let's talk about the keyword super:

Super stands for the parent class, and it is similar to the usage of this.

This represents a reference to an object in this class, and super represents the space to which the parent class belongs.

In general, if an attribute is defined in the parent class, there is no need to define the attribute with the same name in the subclass. Direct inheritance is fine.

Subclasses can only directly access non-private content of the parent class. If you want to access private content, use other methods. Subspaces can access at least two spaces.

Inheritance in the member function:

* When the parent and child classes have the same method, a function is generated, called rewriting (overwriting, overwriting).

* English Override

Rewrite notes:

* 1. When a subclass overrides a parent class, you must ensure that the permissions of the override method must be greater than or equal to the permissions of the overridden method.

* The order of permission modifier permissions: private default protected public

* 2. When the rewrite method has static decoration, static can only rewrite static or be statically rewritten.

*

The difference between rewriting and overloading

* 1. Override override overload is overload

* 2. Overload requirements are in the same class.

* Rewriting is required in parent-child class inheritance relationships.

* 3. The overloaded method name is the same, the parameter list is different, and the return value is not required.

* The override method method has the same name, the same parameter list, and the same return type.

* 4. The overload permission modifier is not required

* Rewrite permission requirements: The permissions of the override method must be greater than or equal to the permissions of the method being overridden.

When rewriting a method, I want to retain the original function while still wanting to add new functions.

At this point you can add a code to the overridden method: super.Xxx()--" to preserve the functionality of the parent class.

Insulation Tubes

Insulation Tubes,Pvc Pipe Insulation,Insulated Pex Pipe,Rubber Insulation Tube

Shenzhen Huiyunhai Tech.Co., Ltd. , https://www.cablesleevefactory.com