Interfaces and inheritance

An interface is a collection of properties, methods, and events with just a declaration and no definition. We use them in programming to group together a set of functionalities that must be implemented in classes that, theoretically, are of the same basic type.

Let's look at an example of a car. In a real-world scenario, any implementation of the Car class must implement certain common basic features such as driving, stopping, and accelerating. Along with those, any object that is classified as a car will also have certain features specific to the make of the car, such as Honda or Nissan.

In the preceding example, an interface could help to promote code reuse and maintain structure across all types of Car. What we can do in this case is to declare Car as an interface that all car derivatives, such as Nissan or Honda must implement.

Similar to an interface, we can also implement inheritance in a C# application. In inheritance, we can define classes with certain methods and properties, which can then be inherited in a child class. In the next subsections, we will look at how interfaces and inheritance are implemented in a C# application.

Inheritance is one of the main pillars of OOP programming. In Chapter 3, Understanding Object-Oriented Programming, we will look into more advanced features related to inheritance and understand how it works.
..................Content has been hidden....................

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