Java Learning notes-constructors

Source: Internet
Author: User
  1. Objective:
Data initialization is very important, uninitialized data is not expected in many cases, many forgotten to initialize the data is the cause of the error. The constructor provides a place to initialize the code of the initialized data in the constructor. Of course, you can not write in the constructor, you can customize a method to call, but the convenience of the constructor is that as long as the object is created, the constructor is automatically called, ensuring that the call is not forgotten. This is the fundamental purpose of using constructors. If there are special cases, such as when creating an object, initialization can cause performance problems, such as time-consuming operations such as connecting to a database, it is not necessary to write the initialization code of the database within the constructor function. Code is written by people, recognize the meaning of the constructor, need not to write in the constructor to see the actual situation, because the general use of constructors are very convenient and reduce errors, so Java syntax in support of the constructor of the machine

2. Features:

(1) The method name of the construction method must be the same as the class name. (2) The constructor method does not have a return type and cannot be defined as void, and the method type is not declared before the method name. (3) The main function of the construction method is to accomplish the initialization of the object, which can pass the parameters that define the object to the domain of the object. (4) A class can define multiple construction methods, and if a constructor is not defined when the class is defined, the compiled system automatically inserts a default constructor with no parameters, which does not execute any code. (5) The construction method can be overloaded, with the number of parameters, type, order. 3. Inheritance (1) subclasses inherit only the default (default) constructor of the parent class, the invisible parameter constructor. If the parent class does not have a default constructor, the subclass cannot inherit the default constructor from the parent class. (2) The parent class inherits from the parent class the default constructor of the subclass, and cannot be the default constructor for the child class. (3) When creating an object, first call the parent class default constructor to initialize the object, and then call the subclass itself to define the constructor. (4) If a subclass wants to invoke a non-default constructor of the parent class, it must be implemented using super. (5) The subclass must call the constructor of the parent class. The default constructor of the parent class can be called automatically by the system, and if the parent class does not have a default constructor, the subclass constructor must call the parent class's constructor through Super.

Java Learning notes-constructors

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.