Characteristics of variables in the child-parent class of the inheritance of Java learning

Source: Internet
Author: User

After the child parent class appears, the class member features:

Make a clear list of members in a class:
1. Variables

2. Functions

3. Constructors

First, look at 1. Variables:

If a non-private member variable with the same name appears in the child class,

Subclass to access the variables in this class, use this

Subclass to access a variable of the same name in the parent class, use super

The use of super is almost consistent with the use of this

This represents a reference to this class of objects

Super represents a reference to a parent class object

Second, look at 2. function:

When a child class has a function that is exactly the same as the parent class, when the subclass object calls the function, the contents of the subclass function are run as if the parent class's function is overwritten.

This condition is another property override (overwrite) of the function

When the subclass inherits the parent class, inheriting the function of the parent class into the subclass, but the subclass has the function, but the content of the function is inconsistent with the parent class, it is not necessary to define the new function, but to use the override special, preserve the function definition of the parent class, and rewrite the function content.

Covered:

1. Subclasses override the parent class and must guarantee that the subclass permission is greater than or equal to the parent class permission to overwrite, or the compilation fails

2. Static can only override static

Attention:

Overloading: See only the parameter list of functions with the same name

overriding: The Child parent class method is identical

Finally, look at 3. Constructors:

When a child class object is initialized by a row, the constructor of the parent class also runs, because the constructor of the subclass defaults to the first line with an implicit statement super ();

Super (): the constructor that accesses the hollow parameter of the parent class (that is, the parameterless constructor), and all constructors in the subclass default to the first row are super ();

Why must subclasses access constructors in the parent class?

Because the data subclasses in the parent class can be obtained directly, the subclass object, when established, needs to see how the parent class initializes the data, so the subclass accesses the constructor in the parent class first when the object is initialized. If you want to access the constructor specified in the parent class, you can specify it by manually defining the Super statement.

NOTE: The super statement must be defined in the first row of the subclass constructor (the first row conflicts with this, there is no super, the first row calls this, the other constructors in this class are called, and the first default statement of the other constructors in this class has super, You can still access the parameterless constructor of the parent class)

The instantiation process for subclasses:

Conclusion: All constructors of subclasses have access to the constructor of the parent hollow parameter by default. Because each constructor of a subclass has an implicit super () in the first line of discouraged;

When the constructor for a parent class does not have an empty argument, the subclass must manually specify the constructor to access the parent class by using the Super statement form. Of course, the first row of the subclass's constructor can also manually specify the this statement to access the constructor in this class, at least one of the constructors in the subclass will access the constructor in the parent class

Characteristics of variables in the child parent class of the inheritance of Java learning

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.