- Hands-On Object:Oriented Programming with C#
- Raihan Taher
- 254字
- 2021-07-02 12:44:42
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.
- 數字媒體應用教程
- 無代碼編程:用云表搭建企業數字化管理平臺
- Mastering Ubuntu Server
- Mastering Unity Shaders and Effects
- 重學Java設計模式
- Nginx實戰:基于Lua語言的配置、開發與架構詳解
- 從零開始學C語言
- Android玩家必備
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- Go語言編程
- Building Serverless Architectures
- Kubernetes進階實戰
- Spark技術內幕:深入解析Spark內核架構設計與實現原理
- 透視C#核心技術:系統架構及移動端開發
- Laravel 5.x Cookbook