Getting started with the Java language (13): The problem of constructing methods in the Java language inheritance

Source: Internet
Author: User
Tags constructor inheritance

Tutorial (11), the basic concepts of inheritance in the Java language are understood. The inheritance of classes and classes in Java is a single inheritance, with the main purpose of reuse. The subclass object can reuse the permission-allowed properties and methods in the parent class, so there is a certain call relationship between the constructor method of the subclass and the constructor method of the parent class, which is described in detail in this article.

First, you need to remember the fact that any of the constructor methods of a subclass will first call a constructor of the parent class. such as the construction method in subclass trainer:

Public Trainer () {}

Although none of the code in the method body of this constructor is written, it also invokes the constructor method of the parent class employee, which by default invokes the parameterless construction method in employee:

Public Employee () {}

As for why the default call is the parameterless construction method, there is a later explanation.

That is, any constructor of any subclass must call a constructor of the parent class, but which constructor will be invoked, depending on how the subclass constructs the method. If the first statement of a subclass constructor is not a super statement, then the default constructor of the parent class without parameters is invoked, as shown in the code above. If the Trainer class has the following construction method:

Public Trainer (string name, double salary, string course) {This.course = course;}

This constructor is also the parameterless construction method of the employee first invoked, because the first statement is not a super statement. However, this construction method is apparently intended to initialize Name,salary,course, where the initialization of name and salary can invoke this constructor in employee:

Public Employee (String name, double salary) {this.name = name; this.salary = salary;}

How can you specify that a constructor for employee should be invoked first in the trainer construction method, rather than the default invocation? Use Super.

Note that the super statement must be placed in the first statement.

Public Trainer (string name, double salary, string course) {Super (name, salary); this.course = course;}

To sum up, in inheritance, there are several things to note about construction methods:

1, any constructor of the subclass will call one of the constructor methods of the parent class.

2. If the constructor of a subclass is not a super statement, then the parameterless construction method of the parent class is invoked, with the same effect as the first statement, Super (). Therefore, in the parent class, it is best to preserve an parameterless construction method.

3. In the first statement of the subclass construction method, you can write a super statement that specifies any construction method that needs to be invoked, mainly by the number and type of arguments. Understanding these points of knowledge, beginners should be more aware of the construction methods in inheritance, in the next tutorial, will gradually understand the inheritance in the overlay, polymorphism and other concepts.

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.