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

Inheritance

The word inheritance means receiving or deriving something from something else. In real life, we might talk about a child inheriting a house from his or her parents. In that case, the child has the same power over the house that his parents had. This concept of inheritance is one of the pillars of OOP. In programming, when one class is derived from another class, this is called inheritance. This means that the derived class will have the same properties as the parent class. In programming terminology, the class from which another class is derived is called the parent class, while the classes that inherit from these are called child classes.

Let's look at an example:

public class Fruit {
public string Name { get; set; }
public string Color { get; set; }
}

public class Apple : Fruit {
public int NumberOfSeeds { get; set; }
}

In the preceding example, we used inheritance. We have a parent class, called Fruit. This class holds the common properties that every fruit has: a Name and a Color. We can use this Fruit class for all fruits.

If we create a new class, called Apple, this class can inherit the Fruit class because we know that an apple is a fruit. The properties of the Fruit class are also properties of the Apple class. If the Apple inherits the Fruit class, we don't need to write the same properties for the Apple class because it inherits these from the Fruit class.

主站蜘蛛池模板: 定州市| 九龙县| 吉林市| 慈利县| 偏关县| 丹凤县| 永兴县| 蛟河市| 连平县| 嘉峪关市| 乐至县| 射阳县| 大渡口区| 舞钢市| 亚东县| 辽源市| 海安县| 江陵县| 枣阳市| 天峻县| 集贤县| 建昌县| 乐平市| 分宜县| 洛阳市| 江门市| 桑植县| 二连浩特市| 江永县| 中方县| 方城县| 阿荣旗| 开江县| 西畴县| 株洲市| 邢台县| 中宁县| 阜南县| 子洲县| 泰安市| 苏尼特右旗|