- Hands-On Object:Oriented Programming with C#
- Raihan Taher
- 270字
- 2021-07-02 12:44:42
Abstraction
If something is abstract, it means that it doesn't have an instance in reality but does exist as an idea or concept. In programming, we use this technique to organize our thoughts. This is one of the pillars of OOP. In C#, we have abstract classes, which implement the concept of abstraction. Abstract classes are classes that don't have any instances, classes that implement the abstract class will implement the properties and methods of that abstract class. Let's look at an example of an abstract class, as shown in the following code:
public abstract class Vehicle {
public abstract int GetNumberOfTyres();
}
public class Bicycle : Vehicle {
public string Company { get; set; }
public string Model { get; set; }
public int NumberOfTyres { get; set; }
public override int GetNumberOfTyres() {
return NumberOfTyres;
}
}
public class Car : Vehicle {
public string Company { get; set; }
public string Model { get; set; }
public int FrontTyres { get; set; }
public int BackTyres { get; set; }
public override int GetNumberOfTyres() {
return FrontTyres + BackTyres;
}
}
In the preceding example, we have an abstract class called Vehicle. It has one abstract method, called GetNumberOfTyres(). As it is an abstract method, this has to be overridden by the classes that implement the abstract class. Our Bicycle and Car classes implement the Vehicle abstract class, so they also override the abstract method GetNumberOfTyres(). If you take a look at the implementation of these methods in the two classes, you will see that the implementation is different, which is due to abstraction.
- HTML5+CSS3王者歸來
- Node.js 10實戰
- Python自然語言處理實戰:核心技術與算法
- 自己動手實現Lua:虛擬機、編譯器和標準庫
- C語言程序設計基礎與實驗指導
- Bootstrap Essentials
- Getting Started with SQL Server 2012 Cube Development
- 快速入門與進階:Creo 4·0全實例精講
- Natural Language Processing with Python Quick Start Guide
- Distributed Computing in Java 9
- Java 9 Programming By Example
- .NET 4.0面向對象編程漫談:應用篇
- 百萬在線:大型游戲服務端開發
- INSTANT Apache Hive Essentials How-to
- Python第三方庫開發應用實戰