Call relationship between child classes and parent classes in Java

Source: Internet
Author: User

Before speaking, some basic knowledge should be available: 1. if no constructor is defined in a class, the compiler automatically adds the default non-argument constructor during compilation: [java] public ClassName {} 2. the difference between this and super. 3. Each class is directly or indirectly a subclass of the Object. The Object has only one construction method without parameters. 4. the compiler will implicitly Add the default no-argument constructor of the parent class in the first line of each constructor, that is, add super (). Easy for beginners: [java] class Employee extends Object {public Employee (int id) {}} class Teacher extends Employee {public Teacher () the above code will produce a compilation error: [plain] Implicit super constructor Employee () is undefined. must explicitly invoke another constructor because the parent class defines a constructor with parameters, the compiler does not add a default non-argument constructor, however, because the constructor of the subclass does not explicitly call a constructor of the parent class, the compiler automatically adds the super () method, but the parent class does not have any default constructor, therefore, the system will prompt that the default construction method without parameters is undefined. The modified code: [java] class Employee {public Employee (int id) {}} class Teacher extends Employee {public Teacher () {super (10, A constructor of the parent class is explicitly called in the constructor of the subclass, so the compiler does not automatically add the super () method. Access modifier of constructor: it can be modified by public, protected, default, and private. However, for private, subclass cannot call the constructor of this class. Multi-level calling of the parent class constructor: Suppose we have such a hierarchy: Object-> Employee-> Teacher-> aggregsor [java] class Employee {public Employee () {System. out. println ("Employee constructor called") ;}} class Teacher extends Employee {public Teacher () {System. out. println ("Teacher constructor called") ;}} class extends sor extends Teacher {public extends sor () {System. out. println ("extends sor constructor called") ;}} public class Test {p Ublic static void main (String args []) {Employee p = new worker sor () ;}} print the result: [plain] Employee constructor called Teacher constructor called pointer sor constructor called when creating the pointer sor object (new pointer sor (), the class's no-argument constructor is first found, and then super () is first called () method: Call the non-parameter constructor of the Teacher Class, call the non-parameter constructor of the Employee, and call the non-parameter constructor of the Object. Finally, print the information again.

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.