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

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.

主站蜘蛛池模板: 峨眉山市| 景泰县| 南康市| 镇远县| 宁陵县| 施秉县| 辛集市| 丹凤县| 安吉县| 新田县| 吴川市| 大连市| 公主岭市| 青阳县| 武山县| 三江| 临沂市| 凤城市| 七台河市| 曲阜市| 莱芜市| 凌源市| 鄂托克前旗| 昌邑市| 商河县| 奇台县| 新河县| 鲜城| 酒泉市| 临澧县| 嘉定区| 呈贡县| 梅州市| 轮台县| 淅川县| 宝坻区| 永安市| 孟村| 惠安县| 化州市| 铅山县|