- 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.
- Data Visualization with D3 4.x Cookbook(Second Edition)
- C++程序設計(第3版)
- PWA入門與實踐
- 零基礎輕松學SQL Server 2016
- Python:Deeper Insights into Machine Learning
- Hands-On Neural Network Programming with C#
- 智能手機故障檢測與維修從入門到精通
- OpenCV with Python Blueprints
- IPython Interactive Computing and Visualization Cookbook
- MySQL 8從零開始學(視頻教學版)
- Mastering PowerCLI
- 零基礎學Java第2版
- 從零開始學算法:基于Python
- 網絡綜合布線與組網實戰指南
- Java EE 7 Development with WildFly