C# Interview Questions

36 Questions
C# Programming

C# Programming

Web DevelopmentFrontendBackendGame Dev

Question 26

Explain the difference between abstract class and interface.

Answer:

Both abstract classes and interfaces are used to define contracts that other classes can implement, but they serve different purposes and have distinct characteristics. Understanding the differences between them helps in choosing the right tool for the job.

Abstract Class

  1. Definition:

    • An abstract class is a class that cannot be instantiated on its own and is meant to be inherited by other classes. It can contain abstract methods (without implementation) and concrete methods (with implementation).
  2. Members:

    • Can contain fields, properties, methods, events, and constructors.
    • Can include abstract methods (which must be implemented by derived classes) and fully implemented methods.
    • Can have access modifiers (public, protected, private, etc.) for its members.
  3. Inheritance:

    • A class can inherit only one abstract class because C# does not support multiple inheritance for classes.
    • Useful when there is a clear hierarchical relationship and you want to share code among multiple derived classes.
  4. Constructors:

    • Can have constructors, which can be called when a derived class is instantiated.
  5. Usage:

    • Suitable for situations where you need to provide a common base class with shared code and some methods that must be overridden in derived classes.

Example:

public abstract class Animal
{
    public abstract void MakeSound(); // Abstract method

    public void Sleep() // Concrete method
    {
        Console.WriteLine("Sleeping...");
    }
}

public class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Bark");
    }
}

Interface

  1. Definition:

    • An interface defines a contract that implementing classes must adhere to. It can contain method signatures, properties, events, and indexers, but no implementation.
  2. Members:

    • Cannot contain fields or constructors.
    • All members are implicitly public and abstract (cannot have any implementation, although default interface methods with implementation were introduced in C# 8.0).
  3. Inheritance:

    • A class or struct can implement multiple interfaces, allowing for more flexible and modular designs.
    • Useful for defining capabilities or behaviors that can be implemented by any class, regardless of its position in the class hierarchy.
  4. Constructors:

    • Cannot have constructors.
  5. Usage:

    • Suitable for defining a set of methods and properties that one or more classes must implement. It is often used for polymorphism.

Example:

public interface IAnimal
{
    void MakeSound(); // Interface method (implicitly public and abstract)

    void Sleep(); // Interface method (implicitly public and abstract)
}

public class Dog : IAnimal
{
    public void MakeSound()
    {
        Console.WriteLine("Bark");
    }

    public void Sleep()
    {
        Console.WriteLine("Sleeping...");
    }
}

Key Differences

Feature Abstract Class Interface
Definition Can contain both abstract and concrete methods Can contain only method signatures and properties (with no implementation)
Fields Can have fields Cannot have fields
Constructors Can have constructors Cannot have constructors
Access Modifiers Can have access modifiers All members are implicitly public
Inheritance Supports single inheritance Supports multiple inheritance
Default Implementation Can provide default implementation for methods Cannot provide implementation (except default methods in C# 8.0)
Usage Use when you need a base class with shared code Use to define a contract for classes to implement

When to Use Each

  • Use Abstract Class when:

    • You need to provide a common base class with shared code that multiple derived classes can use.
    • There is a clear hierarchical relationship (is-a relationship).
    • You need to define protected members that derived classes can access.
  • Use Interface when:

    • You need to define a contract that multiple classes can implement, regardless of their position in the class hierarchy.
    • You want to define capabilities or behaviors that can be added to any class or struct.
    • You need to support multiple inheritance of behavior.

Example of Combining Both

Sometimes, you might use both abstract classes and interfaces together to leverage their strengths:

Example:

public interface IFlyable
{
    void Fly();
}

public abstract class Bird
{
    public abstract void MakeSound();

    public void Sleep()
    {
        Console.WriteLine("Sleeping...");
    }
}

public class Eagle : Bird, IFlyable
{
    public override void MakeSound()
    {
        Console.WriteLine("Screech");
    }

    public void Fly()
    {
        Console.WriteLine("Flying...");
    }
}

In this example, Eagle inherits from the abstract class Bird and implements the IFlyable interface, combining shared behavior and a specific contract.

By understanding the differences between abstract classes and interfaces, you can design your C# applications more effectively, ensuring that your code is both flexible and maintainable.

Recent job openings