Home >Java >javaTutorial >Builders and inheritance
In a class hierarchy, both superclasses and subclasses can have their own constructors.
The superclass constructor is responsible for constructing the part of the object that corresponds to the superclass.
The subclass constructor is responsible for constructing the part of the object that corresponds to the subclass.
In practice, object construction is done in two steps: first, the superclass is constructed and then the subclass.
If only the subclass defines a constructor, the superclass constructor is automatically called to construct the superclass part of the object, using the superclass's default constructor.
Example: Here is a version of the Triangle class that defines a constructor and makes style private, being set by the constructor.
The above is the detailed content of Builders and inheritance. For more information, please follow other related articles on the PHP Chinese website!