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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.95.74