Home >Java >javaTutorial >Builders and inheritance

Builders and inheritance

WBOY
WBOYOriginal
2024-09-10 06:30:381167browse
  • 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.

Construtores e herança

Construtores e herança

The above is the detailed content of Builders and inheritance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn