Java's construction method chain

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/Mr_KkTian/article/details/70226561

1. Introduction

When you construct an instance of a class, you invoke the constructor of all the parent classes along the inheritance chain. When you construct an object of a subclass, the subclass constructor method first calls the constructor of its parent class before completing its own task. If the parent class inherits from another class, the parent class construction method will then call its own constructor of the parent class before completing its own task. This process continues until the last constructed method along the inheritance architecture is called.

2. Example
 Public classFacultyextendsemployee{ PublicFaculty () {System.out.println ("(3) performs Faculty ' s tasks") ; }     Public Static voidMain (string[] args) {Faculty FAC=NewFaculty (); }}classEmployeeextendsPerson { PublicEmployee () {System.out.println ("(2) performs Employee ' s tasks"); }}classPerson { PublicPerson () {System.out.println ("(1) performs person ' s tasks"); }}

Operation Result:
(1) Performs person ' s tasks
(2) Performs Employee ' s tasks
(3) Performs Faculty ' s tasks

Java's construction method chain

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.