官术网_书友最值得收藏!

Inheritance

Inheritance in programming is the same as in real life. A girl inherits from her mother, who inherits from her mother, and so on. The following diagram shows inheritance in the real world and how it relates to OOP:

Inheritance examples

On the left-hand side of the preceding diagram, a Grandmother passes down traits to the Daughter, who passes down traits to the Granddaughter. So, the Granddaughter inherits from both the Daughter and Grandmother. The same inheritance concept applies to our Bicycle class, which inherits from both the Two-Wheeled class and the Vehicle class. This is a powerful schema as it avoids class bloating. As an example, if the Vehicle class has data elements such as year, make, and model, those elements are automatically part of the Two-Wheeled and Bicycle classes. We will see an example of this later on in this chapter.

主站蜘蛛池模板: 增城市| 泽州县| 抚宁县| 玉溪市| 平罗县| 宣化县| 大连市| 阿瓦提县| 洛川县| 右玉县| 青铜峡市| 青川县| 邛崃市| 开阳县| 武宣县| 康定县| 永泰县| 泰安市| 中山市| 永泰县| 灵武市| 库车县| 连平县| 安龙县| 迁西县| 南澳县| 舟曲县| 湘乡市| 枞阳县| 长治市| 邵阳县| 盱眙县| 二连浩特市| 唐河县| 册亨县| 都江堰市| 石嘴山市| 越西县| 襄垣县| 大竹县| 电白县|