The role that the parent class's constructor plays in the life of the object

Source: Internet
Author: User

When a new object is created, all inherited constructors are inherited

    1. This means that each parent class has a constructor (because each class has at least one constructor), and each constructor executes when the object is created.
    2. Executing The new instruction is a big event because he will start a chain reaction to the constructor. Also, even abstract classes have constructors. Although you cannot perform a new operation on an abstract function, the abstract function is also the parent class, so its constructor is executed when the instance is created by a specific subclass.
    3. Call the constructor portion of the parent class with super in the constructor (note: Calling the super () method is the only way to call the parent class constructor). Remember that subclasses inherit methods based on the state of the parent class (that is, the instance variables of the parent class )

Example

  supplement : As shown in the example, hippo objects Is-a Animal also is-a Object , if you need to create hippo , you also need to create a animal and object part of , such a process is called " constructor chain (constructor Chaining )".

1  Public class Animal {2     Public Animal () {3        System.out.println ("Maka a Animal");   4    }5 }

1  Public class extends animal{2    Public Hippo () {3       // super (); Add by default, call parent class 4       System.out.println ("Make a Hippo"); 5   }6 }

1  Public classTesthippo {2 3      Public Static voidMain (string[] args) {4         //TODO auto-generated Method Stub5System.out.println ("Starting ...");6Hippo hippo=NewHippo ();7     }8 9}

Sequential Execution

    1. First, the program executes Action for New Hippo () , hippo () 's constructor goes into the stack block at the top of the stack
    2. hippo () calls the constructor of the parent class to cause animal () The constructor of the enters the top of the stack
    3. animal () calls the constructor of the parent class to cause object () constructor enters top of stack
    4. object () completes, its stack is quickly ejected, and then continues to execute Animal ()
    5. and so on ...

Why do subclasses have to access constructors in the parent class?

    • Because the subclass of data in the parent class can be obtained directly, the sub-class caprice, when established, needs to look at how the parent class initializes the data, so that when the object is initialized, the child class first accesses the constructor in the parent class
    • If you want to access the constructor specified by the Frey, you can specify the call by manually defining the Super statement

Plus Example:

1 classfu{2     intX =30;3  Fu ()4     {   6  System.out.println (x);7     }8Fu (intx)9    {Ten System.out.println (x); One     } A  } -  classZiextendsFu { - Zi () { the          //Seper (); -          //Super (4); //specifies access to a constructor with a parameter in the parent class -           This(20); -System.out.println ("Zi ..." +x); +      } -Zi (intx) +     { ASystem.out.println ("Zi ..." +x); at      } -       - } -  Public classSingle { -       Public Static voidMain (string[] args) { -Zi z=NewZi (); in          //Zi z1=new Zi (3); -      } to}

Execution Results :

Execution order:

    1. constructor to access the sub-class hollow parameter first
    2. When executing a subclass constructor, because of the existence of an implicit super () , the empty constructor of the parent class is executed first, and the output
    3. Next executes this (), because it points to a constructor with one parameter in this class, so System.out.println ("Zi ..." +x) with one parameter in the execution subclass
    4. Finally executes the System.out.println ("Zi ..." +x)of the next line (note: where X is inherited from the parent class)

The role that the parent class's constructor plays in the life of the object

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.