Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Java inheritance constructor

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how the Java inheritance constructor is used. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Initialize the base class

As mentioned earlier, inheritance is an extension of a subclass to a parent class. The following paragraph is mentioned in "Thinking in Java":

When you create an object that exports a class, the object contains children of a base class. This sub-object is the same as the object you create directly from the base class. The difference between the two is that the latter comes from the outside, while the children of the base class are wrapped inside the object of the exported class.

When we create a subclass object, we call the constructor of the parent class, or even the parent constructor of the parent class. We know that constructors are used to create objects, so there is a sudden doubt: when creating a subclass object, will the parent class object be created first?

After searching the data, we come to the conclusion that:

No. When you create a subclass object, the member variables and methods of the parent class are loaded into memory, and since you want to load, call the parent class constructor to see how the data is initialized, that's all, not the object of the parent class.

Therefore, it can be seen that the subclass object contains the children of the parent class. We know that object initialization is critical. So, how do the children of this parent class initialize correctly? By the way, that's what I'm going to say next: call the base class constructor in the constructor to perform initialization. Note: subclasses cannot inherit the constructor of the parent class, but simply call the initialization code in the base class constructor.

Default constructor

Let's take a look at a simple test code:

Construction in package com.my.pac13;/* inheritance * / public class Person {Person () {System.out.println ("Person ()");}} class Student extends Person {Student () {System.out.println ("Student ()");}} class PrimaryStudent extends Student {PrimaryStudent () {/ / super (); System.out.println ("PrimaryStudent ()");} public static void main (String [] args) {/ / create PrimaryStudent object new PrimaryStudent () } / * Person () Student () PrimaryStudent () * /

With regard to constructors, as we mentioned earlier, any class that does not have an explicit constructor has a default constructor without parameters. Our above example adds printout to the default constructor to make it easier to understand.

What you can see is:

When creating PrimaryStudent, the constructors in both his direct parent class Student and his indirect parent class Person are called, and as you can see, are "top-down". The parent class has completed initialization before the subclass constructor can access it.

If the subclass does not explicitly call the parent class's constructor, the parent's default (no parameter) constructor is automatically called.

Constructor with parameters

In the previous code, each class contains a default constructor, the subclass object is created from top to bottom, and the subclass calls the parent class's parameterless constructor by default. So, assuming that the parent class does not happen to have a parameterless constructor or you are about to call the parent's parameterized constructor, our super keyword is needed. (it will be summarized after the super keyword.)

We directly make a little modification on the original basis and test it.

Package com.my.pac13;/* calling the base class constructor is the first thing to do in the subclass constructor * / public class Person {/ / does not have a default constructor Person (String name) {System.out.println ("Person ()\ t" + name);}} class Student extends Person {/ / does not have a default constructor and explicitly calls the Student (String n) {/ / super keyword to call the parent class's constructor super (n) with the super keyword System.out.println ("one parameter Student\ t" + n);} Student (String nMagneString m) {/ / this keyword calls the overloaded constructor this (n) in the same class; System.out.println ("two parameters student ()\ t" + m) }} class PrimaryStudent extends Student {/ / implicitly calls the parent class constructor without parameters, but the parent class does not, so use super to explicitly call PrimaryStudent () {/ / No error super ("hello"); System.out.println ("PrimaryStudent ()");}} class ExtendsTest {public static void main (String [] args) {new Person ("the shy"); System.out.println ("*") New Student ("rookie"); System.out.println ("*"); new Student ("the shy", "rookie"); System.out.println ("*"); new PrimaryStudent (); System.out.println ("*") }} / * Person () the shy*Person () rookie-parameter Student rookie*Person () the shy-parameter Student the shy two-parameter student () rookie*Person () hello-parameter Student helloPrimaryStudent () * /

This is the object being created to call an overloaded constructor in the same class, see my previous article: this of the Java keyword. Super uses a method similar to this when calling the constructor. (but super is fundamentally different from this itself. Super is not a reference to an object! Both super and this statements must appear on the first line, which means that there can only be one of them in a constructor

The child class calls the parent class constructor

Whether or not the superstatement is used to invoke the initialization code of the parent class constructor, the subclass constructor always invokes the parent class constructor in advance! This must be kept in mind!

On the first line, subclass constructor An explicitly uses super to call parent class constructor B in the format super (parameter list), selecting the corresponding parent class constructor based on the parameter list.

/ / parent class Person (String name) {System.out.println ("Person ()\ t" + name);} / subclass Student (String n) {/ / super keyword calls parent class constructor super (n); System.out.println ("one parameter Student\ t" + n);}

Subclass constructor A first invokes this class's overloaded constructor B with this, and then B invokes the parent class constructor.

/ / parent class Person (String name) {System.out.println ("Person ()\ t" + name);} / subclass Student (String n) {/ / super keyword calls the constructor of the parent class super (n); System.out.println ("one parameter Student\ t" + n);} Student (String nline string m) {/ / this keyword calls constructor this (n) overloaded in the same class; System.out.println ("two parameters student ()\ t" + m);}

When there is no super and this in the subclass constructor, the system implicitly calls the parent class's parameterless constructor, and if there is no parameter, an error is reported.

/ / implicitly call the parent class constructor without parameters, but the parent class does not, so use super to explicitly call PrimaryStudent () {/ / there is no error super ("hello"); System.out.println ("PrimaryStudent ()");}

To sum up:

When the subclass constructor is called to initialize the subclass object, the parent constructor always executes before the subclass constructor. Even, the parent class of the parent class executes before the parent class. All the way back to the constructor of the superclass Object class of all classes.

Thank you for reading! This is the end of this article on "how to use Java inheritance Constructor". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report