New a subclass object that does not have a constructor defined, invokes the default constructor method of the parent class

Source: Internet
Author: User
As a question: New A subclass that has no constructor method invokes the parameterless constructor method of the parent class. Like the following two classes:
public class Fatherclass {

	private  int age;

	Public fatherclass (int age) {
		super ();
		SYSTEM.OUT.PRINTLN ("Initialization value" + age);
	}
	
	Public Fatherclass () {
		System.out.println ("Fathercalss");
	}
public class Subclass extends Fatherclass {

	int A;
    Boolean flag;
	String str;
	
}

public class Test {public

	static void Main (string[] args) {
		Subclass subclass = new Subclass ();
		System.out.println (SUBCLASS.A);
		System.out.println (Subclass.flag);
		System.out.println (SUBCLASS.STR);
        Subclass.tostring ();
	}

The above three classes are parent, subclass, and test class respectively. The output results are as follows:
FATHERCALSS
0
false
null

As you can see from the results, when a subclass does not define an parameterless construction method, the new object of that class invokes the parameterless construction method of its parent class. Also, the parameters in the subclass are initialized, and the int type is initialized to the 0,boolean type, initialized to False, and the referenced object is initialized to null.


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.