Why do I need to call the constructor of the parent class when I create a new subclass

Source: Internet
Author: User

The subclass constructor calls the parameterless constructor of the parent class by default, and if the parent class does not have a parameterless constructor, it is necessary to explicitly call the parent class's other constructors in the first row of the child-class constructor.

Second, from the perspective of inheritance, you inherit a class, which is equivalent to extending a class to form a more special class, but often we need to turn the subclass upward into a base class for use or for various other purposes.

At this point, if you do not invoke the constructor of the parent class when you generate the subclass object, we will report the variable uninitialized error when we use some of the member variables of the parent class. keep in mind that variable initialization is always done before the constructor calls!

Constructs an object that first calls its constructor method to initialize its member functions and member variables. A subclass has member variables and member methods of the parent, and if not called, the member variables and member methods inherited from the parent class are not properly initialized.

Why do I need to call the constructor of the parent class when I create a new subclass

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.