Constructors in Java cannot be inherited. When a subclass inherits from a parent class, it first runs the parent class constructor. That is to say, when running the subclass, the parent class's constructor will be called first. Constructors are generally used to initialize member properties and member methods.
Constructors in Java cannot be inherited.
(Recommended tutorial: java introductory program)
Analysis:
When a subclass inherits a parent class, it first runs the parent class Constructor, that is to say, when running a subclass, the constructor of the parent class will be "called" first, which is essentially "automatic running".
The meaning of inheritance (extends) is actually "extension". There is no need for a subclass to extend the constructor of the parent class, because every time the subclass is called, the constructor of its parent class will be "automatically run". If If you really need a special form of the subclass constructor, the subclass can just directly modify or overload its own constructor.
Introduction to constructor
The java constructor, also called the construction method, is a special function in java. The function name is the same as and has no return value.
Function
The constructor is generally used to initialize member properties and member methods. That is, after the new object is generated, the properties and methods of the object are called.
(Video tutorial recommendation: java video tutorial)
Characteristics of constructor
1. The function name is the same as the class name;
2 , there is no need to define the return value type; (different from the void type return value, void has no specific return value type; the constructor does not even have a type)
3. You cannot write a return statement; (the return value type is No, there is no need for a return statement)
Note: General functions cannot call constructors, only constructors can call constructors.
The above is the detailed content of Can constructors be inherited in Java?. For more information, please follow other related articles on the PHP Chinese website!