Java basics: subclass-parent class constructor relation, java Constructor

Source: Internet
Author: User

Java basics: subclass-parent class constructor relation, java Constructor

Premise: Parent class: Parent. java

Constructor: default constructor (without parameters) and parameter Constructor (when both parameters exist, you must create a constructor without parameters)

Example: public Parent () {}, public Parent (String param ){}

Subclass: Children. java

Constructor: default constructor (without parameters) and parameter Constructor (when both parameters exist, you must create a constructor without parameters)

Example: public Children () {}, public Children (String param ){}

1. Subclass display calls the parent class Constructor

(1) For example: public Children () {super () ;}, public Children (String param) {super (param );}

Display call. You can specify which constructor of the parent class to call. The keyword "super" must be written in the first line.

2. Subclass implicitly calls the parent class Constructor

(1) For example: public Children () {}, public Children (String param ){}

Implicit call. No parameter constructor is called by default. If no parameter constructor exists, the call must be displayed.

Note: Parent and Child classes refer to instantiated classes (that is, they do not involve interfaces or abstract classes)

The reason why super is written in the first line: the parent class must be constructed first, and the Child class must be constructed. Loading Mechanism problems.

3. this and super

This refers to the current class. this (), this (param) calls the class construction and needs to be placed in the first line, this. variable (variable)/method, etc., System. out. println (this) Description: The toString method is called by default, and the Object method is called or implemented by itself.

Super refers to the parent class. It is called only once in the subclass structure and only called in the last construction of the subclass this call.

Note: inaccurate or missing content. You are welcome to make positive comments ).

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.