Search⌘ K

Implementing Interfaces

Explore how interfaces enable flexible and structured code in C#. Understand how to define and implement interfaces such as IControllableRemotely to share functionality across unrelated classes, ensuring consistent method implementation.

We'll cover the following...

Overview

Let’s see interfaces in action by jumping into the code. We have the ElectricCar class that simulates an electric car. Run the code to see what this class can do.

C#
using System;
namespace Interfaces
{
class Program
{
static void Main(string[] args)
{
var car = new ElectricCar();
Console.WriteLine("We've got a Tesla.");
Console.WriteLine("Attempting to drive it.");
car.Drive();
car.StartTheCar(15);
car.StartTheCar(14);
car.Drive();
car.TurnTheCarOff();
}
}
}

Our car can be started, driven, and turned off. ...