Thursday, October 24, 2024

C# Interface vs Abstract Class


Objectives:
  • To know the situation in which you should use interface instead of abstract class in C#.
  • To know the situation in which you should use abstract class instead of interface in C#.

In C#, both interfaces and abstract classes are used to define contracts for types that implement or derive from them. The choice between using an interface or an abstract class depends on the specific requirements and design considerations of your application.

Abstract classes provide a way to define common behavior and state for related classes, whereas interfaces define contracts which can be implemented by any class.

Here's a situation where using an interface is more appropriate than an abstract class:

Suppose you are designing a system for a car rental service that needs to calculate the rental cost for various types of vehicles, including cars, motorcycles, and bicycles. Each type of vehicle has its own way of calculating the rental cost based on factors like the rental duration, type of fuel, and additional services. You want to provide a consistent way to calculate the rental cost for any vehicle type.

In this scenario, using an interface would be a better choice. You can define an IRentalCostCalculator interface with a method for calculating the rental cost:
public interface IRentalCostCalculator
{
    decimal CalculateRentalCost(int rentalDuration);
}

Each vehicle type (car, motorcycle, bicycle) can implement this interface to provide its specific implementation for calculating the rental cost:
public class Car : IRentalCostCalculator
{
    public decimal CalculateRentalCost(int rentalDuration)
    {
        // Calculate rental cost for a car
        // Based on rental duration and other factors specific to cars
        // ...
    }
}

public class Motorcycle : IRentalCostCalculator
{
    public decimal CalculateRentalCost(int rentalDuration)
    {
        // Calculate rental cost for a motorcycle
        // Based on rental duration and other factors specific to motorcycles
        // ...
    }
}

public class Bicycle : IRentalCostCalculator
{
    public decimal CalculateRentalCost(int rentalDuration)
    {
        // Calculate rental cost for a bicycle
        // Based on rental duration and other factors specific to bicycles
        // ...
    }
}

By using an interface in this scenario, you ensure that each vehicle type provides its own implementation of the rental cost calculation, while still adhering to a common contract defined by the IRentalCostCalculator interface. This allows for flexibility and maintainability in your application's design.

Now, we look at another situation in which you should use abstract class instead of  interface in C#.

An abstract class in C# is typically used when you want to provide a common implementation and define a base structure for derived classes while allowing certain methods to be overridden. 

If you have a situation where you want to provide a default or partial implementation for some methods, an abstract class is appropriate. Abstract classes can have method implementations and member variables in addition to defining abstract methods that must be implemented by derived classes.
For example, consider a scenario where you have a base class representing a shape with a method to calculate area. Different shapes may have different formulas for area calculation, but they all have some common properties and behavior.
public abstract class Shape
{
    public abstract double CalculateArea();
    
    public void DisplayDetails()
    {
        Console.WriteLine("This is a shape.");
    }
}

public class Circle : Shape
{
    private double radius;

    public Circle(double radius)
    {
        this.radius = radius;
    }

    public override double CalculateArea()
    {
        return Math.PI  radius  radius;
    }
}
In this example, the Shape class is an abstract class with a method CalculateArea defined as abstract, which must be implemented by derived classes. It also has a method DisplayDetails with a default implementation, which can be inherited or overridden by derived classes.

Abstract classes are a good choice when you have common behavior that can be shared among multiple related classes, and you want to provide a default implementation for some methods.

No comments:

Post a Comment

Hot Topics