Subclass inherits and invokes the constructor method of the parent class

Source: Internet
Author: User
1. If the subclass does not define a constructor method, then the parameterless constructor method of the parent class is invoked.
2. If a subclass defines a constructor method, whether it is a parameterless or a parameter, when creating an object of a subclass, the parent class is first executed without arguments, and then its own construction method is executed.
3. If a subclass invokes a constructor with a parameter in the parent class, it is possible to make the first statement in the subclass constructor by calling the constructor of the parent class required by the super (parameter).
4. If a constructor invokes another construction method in the class, you can use this (parameter) to cut the statement in the first article of the constructor method.
Plainly: The principle is to call the father first. (No on the default tone, there is a tune, anyway, as long as there is one on it.)
Package test; Class father{String s = "Run constructor method of Father"; public Father () {System.out.println (s);} public Father (St
   Ring str) {s= str;
System.out.println (s); The class Son extends father{String s= "Run constructor method of Son"; the public Son () {//is actually here plus super (), and no Plus is the same Sy
Stem.out.println (s);
   Public Son (String str) {this ();//This calls this () here to invoke the Son () of this class, because there is a super () in Son (), so it can't be added here.
   s = str;
System.out.println (s);
   The public Son (string str1, String str2) {super (str1+ "" +str2);//Because a superclass with parameter super ("---") has been called here, no arguments are automatically invoked.
   s = str1;
System.out.println (s);
   } public class MyClass9 {public static void main (string[] args) {Father obfather1 = new Father ();
   Father obfather2 = new Father ("Hello Father");
   Son obson1 = new Son ();
   Son obson2 = new Son ("Hello Son");
  
Son Obson3 = new Son ("Hello Son", "Hello Father"); =============== Result: Run constructor method of Father Hello Father Run constructor method of Father run ConstrUctor the son Run constructor method of Father run constructor method of son hello son hello son hello Father Son

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.