C# Interview Questions

36 Questions
C# Programming

C# Programming

Web DevelopmentFrontendBackendGame Dev

Question 10

What is abstraction in C#?

Answer:

Abstraction is a core principle of object-oriented programming (OOP) that focuses on hiding the complex implementation details of a system and exposing only the necessary and relevant features. In C#, abstraction allows developers to define abstract classes and interfaces to create a blueprint for other classes to follow, enabling a clear separation between what an object does and how it does it.

Key Concepts of Abstraction

  1. Abstract Classes:

    • An abstract class cannot be instantiated directly and is designed to be inherited by other classes.
    • It can contain abstract methods (without implementation) and non-abstract methods (with implementation).
    • Derived classes must provide implementations for all abstract methods.
  2. Interfaces:

    • An interface defines a contract that implementing classes must adhere to, without providing any implementation.
    • A class or struct can implement multiple interfaces.
    • Interfaces can contain method signatures, properties, events, and indexers.

Benefits of Abstraction

  1. Simplification:

    • Abstraction reduces complexity by allowing developers to focus on high-level functionality without worrying about low-level details.
  2. Modularity:

    • It promotes modularity by dividing a system into distinct components, each with a clear and defined responsibility.
  3. Maintainability:

    • Abstraction makes the codebase easier to maintain and extend. Changes in implementation details do not affect the abstract definitions, allowing for independent development and maintenance of components.
  4. Reusability:

    • Abstract classes and interfaces enable code reuse by providing a common base for multiple classes to share and extend functionality.

Example of Abstraction in C#

Abstract Class Example

public abstract class Animal
{
    // Abstract method (does not have a body)
    public abstract void MakeSound();

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

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

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

class Program
{
    static void Main()
    {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.MakeSound();  // Outputs: Bark
        myDog.Sleep();      // Outputs: Sleeping...

        myCat.MakeSound();  // Outputs: Meow
        myCat.Sleep();      // Outputs: Sleeping...
    }
}

In this example:

  • The Animal abstract class defines an abstract method MakeSound and a regular method Sleep.
  • The Dog and Cat classes inherit from Animal and provide implementations for the MakeSound method.
  • The Sleep method is inherited by both Dog and Cat without modification.

Interface Example

public interface IVehicle
{
    void StartEngine();
    void StopEngine();
}

public class Car : IVehicle
{
    public void StartEngine()
    {
        Console.WriteLine("Car engine started");
    }

    public void StopEngine()
    {
        Console.WriteLine("Car engine stopped");
    }
}

public class Motorcycle : IVehicle
{
    public void StartEngine()
    {
        Console.WriteLine("Motorcycle engine started");
    }

    public void StopEngine()
    {
        Console.WriteLine("Motorcycle engine stopped");
    }
}

class Program
{
    static void Main()
    {
        IVehicle myCar = new Car();
        IVehicle myMotorcycle = new Motorcycle();

        myCar.StartEngine();       // Outputs: Car engine started
        myCar.StopEngine();        // Outputs: Car engine stopped

        myMotorcycle.StartEngine(); // Outputs: Motorcycle engine started
        myMotorcycle.StopEngine();  // Outputs: Motorcycle engine stopped
    }
}

In this example:

  • The IVehicle interface defines the contract for StartEngine and StopEngine methods.
  • The Car and Motorcycle classes implement the IVehicle interface, providing specific implementations for the methods.

Conclusion

Abstraction in C# is a powerful concept that helps manage complexity, improve code maintainability, and promote reusability. By using abstract classes and interfaces, developers can define clear contracts and blueprints for objects, focusing on what an object should do rather than how it does it. This separation of concerns leads to cleaner, more modular, and easily maintainable code.

Recent job openings