All about Java creation and constructor of Java objects (2)

Source: Internet
Author: User

If a Class A inherits from another class B, how does the constructor operate when the Class A object is created? After a class inherits from another class, this class will contain all the instance variables and methods of the parent class. It also includes private instance variables in the parent class. Although you cannot directly access the instance variables marked as private in the parent class in the subclass, however, the getter and setter defined in the parent class can still be used for operations. Some other methods inherited from the parent class may also use some instance variables in the parent class. If the instance variables in the parent class are not initialized, A problem may occur during initialization of Class A. Therefore, Class B (parent class) should be initialized before class A objects are initialized.

Public class animal {public animal () {system. out. println ("I am making a new animal") ;}} public class dog extends animal {public dog () {system. out. println ("I am making a new dog") ;}} public class testdog {public static void main (string [] ARGs) {system. out. println ("I am starting"); DOG d = new dog () ;}/// this code, after we run the testdog class, the correct output should be // I am starting // I am making a new animal // I am making a new dog

We can see that when creating a dog object, we first completed the animal class constructor, and finally completed the Dog class constructor. We can use the stack to understand the call of this constructor, when we create a new dog, the dog () constructor is pushed into the stack, and then the animal () constructor is pushed into the stack. In fact, all classes in Java inherit from the object, therefore, the object () constructor is pushed into the stack, that is, the top layer of the stack. Then, the object () constructor is completed. The object () pops up from the stack. Then the animal () constructor is completed and popped up from the stack. Finally, the dog () constructor is completed () constructor and pop up from the stack. So far, the creation of the dog object is complete.
Through the above Code and output, we can see that during the running of the dog constructor, the constructor automatically calls its parent class animal class constructor, the animal constructor actually automatically calls the object class constructor. In fact, JVM adds the super (); statement in the first line of each constructor. Its function is to call the constructor of the parent class. Of course, we can also manually call Super (). For example, when the constructor of the parent class has parameters, we can use super (XX, XX); the constructor of the parent class is called in the constructor of the subclass. However, note that the super () statement must be placed in the first line of the subclass constructor to call the method. Otherwise, an error is reported. Once we manually call Super () JVM, the super () call will not be automatically added to us.
In addition, if I have two or more constructor methods, when one a constructor only adds some additional processing to another B constructor, we may think of code reuse, we can use this (); in the constructor to call the B constructor for code reuse. For example:

Public class dog {
Int height;

Public dog (){
This (40 );
// Other initialization operations can be performed here
// ***** We need to note that this must also be placed in the first line of the constructor. Remember that super () is also required to be placed in the first line,
// So this (); and super (); it cannot be used at the same time *********************************** *****************************
}
Public dog (INT newheight ){
Height = newheight;
}
}

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.