A summary of inheritance relationship learning in Java

Source: Internet
Author: User

Inheritance: Extracts the same content from multiple classes. Defined in a class, other classes simply inherit the class. You can use the properties exposed by the class and the exposed methods.

?

Advantages of inheritance: improve code reusability. Improve the maintainability of your code . Having a relationship between classes and classes is a precondition for polymorphic existence.

?

The disadvantage of inheritance: it improves the coupling between classes and classes.

?

Principle of development: low coupling. High cohesion

Coupling: The relationship between a class and a class

Cohesion: the ability to complete one thing alone.

?

In Java, you simply agree to single inheritance. One class assumes that no other classes are shown to inherit. The default parent class of the class is the object class.

The object class is the parent class for all classes.

?

Java supports multiple layers of inheritance.

?

The public methods of the parent class can access private methods and private members of the parent class.

?

Subclasses can only inherit all non-private members of the parent class (member methods and member variables).

?

Subclasses cannot inherit the constructor of the parent class, but can access the constructor of the parent class through super () keyword.

?

Do not inherit for part of the function.

?

When do you consider inheriting?

Inheritance embodies the relationship of Is-a.

Using false attempts: There are two classes A and B, assuming a is a detailed embodiment of B, or B is a detailed embodiment of a. will be able to use inheritance.

?

In the inheritance. Relationship to member variables: When a member variable of the same name is defined in a child parent class, the member variable in the subclass overrides the member variable in the parent class. A variable with the same name in the parent class can be called through super in a subclass.

?

To access a variable's lookup order in a subclass method:

A: Find within the corresponding local scope of the subclass. Yes, it's for.

? B: In the subclass of the member scope of the search, there is the use.

? C: Find in the scope of the members of the parent class, there is the use.

? Assumptions are not found. The error.

?

In a class, there is a peer relationship between the method and the method, and the method cannot access the other's local variables.

?

The difference between super and this:

???????? This represents a reference to this class of objects

???????? Super represents the label of the parent's storage space, which is equivalent to a reference to the parent class.

This and super are able to access construction methods, member variables, and member methods.

?

The relationship between the inheritance-building methods.

The subclass all constructs the method by default to visit the parent class's non-participating constructor method.

???????? Because subclasses inherit data from the parent class, the data in the parent class may be used, so the initialization of the parent class must be completed before the subclass is initialized.

The first statement of each constructed method is by default: Super

What if there is no construction method in the parent class?

Subclasses use super to show other constructor methods that call the parent class

Subclasses use this to invoke the other constructor methods of this class. Other constructs of this class must also first access the parent class constructs

Be sure to note:

Super (...) or This (...) must now be on the first statement. Otherwise, there will be multiple initialization of the parent data

?

Relationships between inherited member methods

When a member method of the same name is defined in a child parent class, the member method in the child class overrides the Member method in the parent class.

A method with the same name in the parent class can be called through super in a subclass.

Access the search order of a method through the subclass object:

???????? A: Look in the subclass, assuming you find it, call

???????? B: Look in the parent class. If you find it, call

Assumptions are not found. On the error. Does not find the parent class of the parent class ...

A summary of inheritance relationship learning in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.