Understanding C# Interface
Interfaces in C# define a contract that classes must follow. An interface contains definitions for a set of related functionalities that a class or a struct can implement. Interfaces promote a strong separation of concerns and enable flexibility in how specific classes implement shared functionality. Unlike abstract classes, a class can implement multiple interfaces.
Key Topics
- What is an Interface?
- Interface Implementation
- Implementing Multiple Interfaces
- Interface Inheritance
- Interfaces vs Abstract Classes
What is an Interface?
An interface in C# is a completely abstract type that defines a set of method signatures without providing any implementation. Any class that implements the interface must provide its own implementation of all the methods defined in the interface.
Example: Defining an Interface
public interface IMovable
{
// Interface method with no implementation
void Move();
}
public class Car : IMovable
{
// Implementing the Move method
public void Move()
{
Console.WriteLine("The car is moving.");
}
}
class Program
{
static void Main(string[] args)
{
IMovable car = new Car();
car.Move(); // Output: The car is moving.
}
}
Output:
The car is moving.
Explanation: The interface IMovable
defines a method Move()
without providing its implementation. The class Car
implements the Move()
method by providing its specific behavior.
Interface Implementation
When a class implements an interface, it must provide concrete implementations of all the interface's methods. A class can also explicitly implement interface members to control their accessibility.
Example: Implementing an Interface
public interface IDriveable
{
void Start();
void Stop();
}
public class Bicycle : IDriveable
{
public void Start()
{
Console.WriteLine("The bicycle starts moving.");
}
public void Stop()
{
Console.WriteLine("The bicycle stops moving.");
}
}
class Program
{
static void Main(string[] args)
{
IDriveable bike = new Bicycle();
bike.Start(); // Output: The bicycle starts moving.
bike.Stop(); // Output: The bicycle stops moving.
}
}
Output:
The bicycle starts moving. The bicycle stops moving.
Explanation: The class Bicycle
implements the interface IDriveable
by providing implementations for both the Start()
and Stop()
methods. This ensures the bicycle can start and stop, fulfilling the contract defined by the interface.
Implementing Multiple Interfaces
One of the advantages of interfaces is that a class can implement multiple interfaces, allowing for greater flexibility and separation of concerns. This enables a class to take on different roles by implementing various functionalities from different interfaces.
Example: Implementing Multiple Interfaces
public interface IDriveable
{
void Start();
void Stop();
}
public interface IFlyable
{
void Fly();
}
public class Drone : IDriveable, IFlyable
{
public void Start()
{
Console.WriteLine("Drone starts moving.");
}
public void Stop()
{
Console.WriteLine("Drone stops moving.");
}
public void Fly()
{
Console.WriteLine("Drone is flying.");
}
}
class Program
{
static void Main(string[] args)
{
Drone drone = new Drone();
drone.Start(); // Output: Drone starts moving.
drone.Fly(); // Output: Drone is flying.
drone.Stop(); // Output: Drone stops moving.
}
}
Output:
Drone starts moving. Drone is flying. Drone stops moving.
Explanation: The class Drone
implements both the IDriveable
and IFlyable
interfaces, meaning it must provide implementations for the methods defined in both interfaces. This allows the drone to take on multiple behaviors, such as driving and flying.
Interface Inheritance
Interfaces in C# can inherit from other interfaces, just like classes. When one interface inherits from another, it includes all the members of the base interface, and any class implementing the derived interface must implement all inherited members.
Example: Interface Inheritance
public interface IAnimal
{
void Eat();
}
public interface IPet : IAnimal
{
void Play();
}
public class Dog : IPet
{
public void Eat()
{
Console.WriteLine("The dog is eating.");
}
public void Play()
{
Console.WriteLine("The dog is playing.");
}
}
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
dog.Eat(); // Output: The dog is eating.
dog.Play(); // Output: The dog is playing.
}
}
Output:
The dog is eating. The dog is playing.
Explanation: In this example, the IPet
interface inherits from the IAnimal
interface. The class Dog
implements both interfaces by providing concrete implementations for both the Eat()
and Play()
methods.
Interfaces vs Abstract Classes
While both interfaces and abstract classes are used to achieve abstraction in C#, there are some key differences between the two. Abstract classes can contain both abstract and non-abstract methods, whereas interfaces can only define method signatures (until C# 8.0, which introduced default interface methods). Abstract classes can have constructors and fields, while interfaces cannot. Additionally, a class can inherit from only one abstract class but can implement multiple interfaces.
Key Differences:
- An abstract class can have method implementations, while an interface cannot (prior to C# 8.0).
- Classes can implement multiple interfaces but can only inherit from one abstract class.
- Abstract classes can have fields and constructors; interfaces cannot.
Key Takeaways
- Interfaces define a contract for functionality that classes must implement.
- Interfaces enable polymorphism by allowing a class to implement multiple interfaces.
- Classes implementing an interface must provide implementations for all the methods defined in the interface.
- Interfaces can inherit from other interfaces, creating more complex hierarchies.
- Interfaces are different from abstract classes in that they cannot have method implementations or fields (except with default methods from C# 8.0 onward).