Java language depth-constructor inheritance issues)

Source: Internet
Author: User
Document directory
  •  

This is my reading notes and I hope it will help you learn Java.
All codes are tested. test environment:
Java version "1.4.0-RC"
Java (TM) 2 Runtime Environment, Standard Edition (build 1.4.0-rc-b91)
Java hotspot (TM) Client VM (build 1.4.0-rc-b91, mixed mode)
If you find any errors or have any comments, please do not give us any further advice.

Default constructor: The base class is the parent class, and the derived class is the subclass.
Note that because there are first parent classes and then child classes, a parent class must be created before a child class is generated.
Class is generated by constructor, the constructor of the class. Each class has
Constructor. If you did not write any constructor when writing your own class
The compiler automatically generates a default constructor. This default constructor
It is actually empty and does not contain any code. However, when inheritance is involved, the problem arises.
.

If the base class of the parent class has only the default constructor, that is, the compiler automatically generates the base class for you.
In the subclass, only the default constructor is used, so no problem occurs, because when you try to generate
For a subclass instance, you must first execute the constructor of the subclass. However, because the subclass inherits the parent class,
Therefore, the default constructor of the subclass automatically calls the default constructor of the parent class. First, an instance of the parent class is generated,
Then a subclass instance is generated. As follows:

Class base {
}
Class derived extends base {
Public static void main (string [] ARGs ){
Derived d = new derived ();
}
}

The default constructor is explicitly added below:
Class base {
Base (){
System. Out. println ("base constructor ");
}
}
Class derived extends base {
Derived (){
System. Out. println ("derived constructor ");
}
Public static void main (string [] ARGs ){
Derived d = new derived ();
}
}

The execution result is as follows: The base class and the derived class are generated first.
Base Constructor
Derived Constructor

The problem I want to explain is that if the base class has multiple Constructor
Derived class also has multiple constructor. In this case, the constructor In the subclass defaults
What about calling the constructor of the parent class? The answer is to call the default constructor of the parent class.
But instead of the default constructor automatically generated by the compiler, you explicitly
The written default constructor.

Class base {
Base (){
System. Out. println ("base constructor ");
}
Base (int I ){
System. Out. println ("base constructor int I ");
}
}
Class derived extends base {
Derived (){
System. Out. println ("derived constructor ");
}
Derived (int I ){
System. Out. println ("derived constructor int I ");
}
Public static void main (string [] ARGs ){
Derived d = new derived ();
Derived T = new derived (9 );
}
}

D:/Java/thinking/think6> JAVA derived
Base Constructor
Derived Constructor
Base Constructor
Derived constructor int I

If you comment out the base class constructor, an error occurs.

Class base {
// Base (){
// System. Out. println ("base constructor ");
//}
Base (int I ){
System. Out. println ("base constructor int I ");
}
}
Class derived extends base {
Derived (){
System. Out. println ("derived constructor ");
}
Derived (int I ){
System. Out. println ("derived constructor int I ");
}
Public static void main (string [] ARGs ){
Derived d = new derived ();
Derived T = new derived (9 );
}
}

D:/Java/thinking/think6> javac derived. Java
Derived. Java: 10: cannot resolve symbol
Symbol: constructor base ()
Location: class base
Derived (){
^
Derived. Java: 13: cannot resolve symbol
Symbol: constructor base ()
Location: class base
Derived (int I ){
^
2 errors

The constructor In the subclass cannot find the default value in the parent class explicitly written.
Constructor, so an error occurs.

If you do not want the subclass constructor to call the default value in the parent class you explicitly write
What about constructor?
For example:

Class base {
// Base (){
// System. Out. println ("base constructor ");
//}
Base (int I ){
System. Out. println ("base constructor int I ");
}
}
Class derived extends base {
Derived (){
Super (8 );
System. Out. println ("derived constructor ");
}
Derived (int I ){
Super (I );
System. Out. println ("derived constructor int I ");
}
Public static void main (string [] ARGs ){
Derived d = new derived ();
Derived T = new derived (9 );
}
}

D:/Java/thinking/think6> JAVA derived
Base constructor int I
Derived Constructor
Base constructor int I
Derived constructor int I

Super (I) indicates the constructor base (I) of the parent class. Please note that
One is super (I) and the other is super (8 ).
Why ??

Conclusion: If a subclass has multiple constructor functions, the parent class does not have any constructor,
To enable the compiler to automatically generate the code before executing the subclass Constructor
The default constructor of the parent class automatically generated by the interpreter; either at least one explicit
You can call the constructor of a subclass.

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.