What C# feature will you use to keep common behaviors at one place and force clients to implement some others?

Answer: To have common behaviors at one place and force clients to implements others, we should use C# Abstract class and method feature. Abstract class is the class that contains common behaviors/methods and defer implementation of some methods to client’s class in inheritance.

Client class will be referred as a derived class. Client classes will be using the common methods defined in abstract class Car and implement some methods deferred by base class Car.

Let’s understand this scenario by an example and implementation:

In below code, Car class has some common methods – audioSystem() and airbags() that will be used by all client classes.

We will have two more abstract methods in the class Car i.e. mileage()and engine()that will be implemented by all client classes i.e. MarutiErtiga and MarutiBaleno cars.

Note that “abstrcat” is the keyword used before function to defer implementation to client classes.

 

 

Output:

Maruti Ertiga
airbags
audioSystem
17.5 kmpl
1373 cc
Maruti Baleno
airbags
audioSystem
21.4 kmpl
1197 cc