Java BASICS (11) JavaSE edition-inheritance and javase

Source: Internet
Author: User

Java BASICS (11) JavaSE edition-inheritance and javase

 

1. Object-oriented Feature 2: inheritance definition:

An object directly uses the attributes and methods of another object.

Benefits:

1. Provide code reusability.

2. Let the class have a direct relationship with the class, providing a prerequisite for the third feature polymorphism.

Java supports single inheritance. Multi-inheritance is not directly supported, but the multi-Inheritance Mechanism in C ++ is improved.

Single inheritance: a subclass can only have one parent class.

Multi-inheritance: a subclass can have multiple direct parent classes (which are not allowed in java for improvement and are not directly supported. Because multiple parent classes have the same members, call uncertainty occurs, in java, multiple implementations are used.

Java supports multi-layer (Multi-Level) inheritance. (For example, if C inherits B and B inherits A, the inheritance system will appear .)

(Nonsense: When you want to use an inheritance system, 1. view the top-level classes in this class to understand the basic functions of this system. 2. Create the most subclass object in the system and use the function .)

 

When to define inheritance?

When there is a relationship between the class and the class, it is defined as inheritance. (Xxx is a type of yyy; xxx extends yyy)

 

This is used to distinguish a Member from a local variable with the same name.
When the member variables in the Child parent class have the same name, use super to distinguish the parent class.

This is similar to super in usage.

This indicates the reference of a Class Object.
Super: represents a parent space.

Override operation. This is a feature of the function in the Child parent class.

Two features of the function:
1. Heavy Load. In the same class. Overload
2. overwrite. Subclass. Overwrite is also called overwrite. Override

Considerations:
1. When the subclass method overwrites the parent class method, the permission of the subclass must be greater than or equal to the permission of the parent class. (The parent method is not called overwrite)
2. static data can only be overwritten or overwritten.

When do I use overwriting?

When a subclass is extended for a class, the subclass must retain the function declaration of the parent class. To define the special content of this function in the subclass, the override operation is used.

In the subclass constructor, the first line has a default implicit statement. Super ();

Subclass instantiation process: All constructors in the subclass access the constructors with null parameters in the parent class by default.

Why access the constructor in the parent class during subclass instantiation?
This is because the subclass inherits the parent class and obtains the content (attributes) of the parent class. before using the content of the parent class, you must first check how the parent class initializes its own content. Therefore, when constructing an object, the subclass must access the constructor in the parent class. To complete this required action, the super () Statement is added to the constructor of the subclass.

If no null parameter constructor is defined in the parent class, the constructor of the Child class must use super to specify which constructor of the parent class to call. At the same time, if the subclass constructor uses this to call the constructor of this class, super will be absent, because both super and this can only define the first line. So there can only be one. However, it is guaranteed that other constructors in the subclass will certainly access the constructors of the parent class.

Note: The supre statement must be defined in the first line of the subclass constructor. Because the initialization of the parent class must be completed first.

An Object Instantiation process:


For example, Person p = new Person ();
1. The JVM will read the Person. class file in the specified path and load it into the memory. It will first load the parent class of the Person (if there is a direct parent class ).
2. Open up space and allocate addresses in the heap memory.
3. In the object space, default Initialization is performed on the attributes of the object.
4. Call the corresponding constructor for initialization.
5. In the constructor, the first line first calls the constructor in the parent class for initialization.
6. After the parent class is initialized, display and initialize the attributes of the subclass.
7. initialize the subclass constructor.
8. After initialization, assign the address value to the referenced variable.

Split graph (steps are as follows ):

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.