4. How do you implement inheritance in C#?

Basic

4. How do you implement inheritance in C#?

Overview

In C#, inheritance is a fundamental concept that allows a class (derived class) to inherit fields, methods, and other members from another class (base class). This mechanism promotes code reusability and establishes a relationship between the parent and child classes. Understanding how to implement and utilize inheritance is crucial for building robust and scalable object-oriented applications in C#.

Key Concepts

  1. Base and Derived Classes: The core of inheritance, where one class (derived) inherits from another (base).
  2. Access Modifiers: Control access to class members and determine how inheritance is applied.
  3. Polymorphism: Utilizing inherited methods in derived classes, potentially overriding them to customize or extend functionality.

Common Interview Questions

Basic Level

  1. What is inheritance and how do you use it in C#?
  2. Can you show a simple example of inheritance in C#?

Intermediate Level

  1. How does access modification affect inheritance in C#?

Advanced Level

  1. Explain how polymorphism works in the context of inheritance in C#.

Detailed Answers

1. What is inheritance and how do you use it in C#?

Answer: Inheritance in C# is a mechanism by which one class (called the derived class) can inherit the fields, methods, and properties of another class (called the base class). This allows for code reuse and hierarchical classification. In C#, a class uses the : symbol to inherit from another class.

Key Points:
- Inheritance promotes code reusability.
- It establishes an "is-a" relationship between the derived and base class.
- C# does not support multiple inheritance directly but can achieve similar functionality using interfaces.

Example:

public class Vehicle // Base class
{
    public string Brand = "Ford";
    public void honk()             // Base class method
    { 
        Console.WriteLine("Tuut, tuut!"); 
    }
}

public class Car : Vehicle // Derived class
{
    public string Model = "Mustang"; // Derived class field
}

class Program
{
    static void Main(string[] args)
    {
        Car myCar = new Car();
        myCar.honk();
        Console.WriteLine(myCar.Brand + " " + myCar.Model); // Accessing base and derived class members
    }
}

2. Can you show a simple example of inheritance in C#?

Answer: Sure, the following example demonstrates basic inheritance where a Car class inherits from a Vehicle class.

Key Points:
- The Car class automatically inherits all members (fields, methods) from the Vehicle class.
- The derived class can add its own unique members.
- The derived class can use the base class members as if they were declared inside the derived class.

Example:

public class Vehicle
{
    public string LicensePlate = "XYZ 1234";
    public void Drive()
    {
        Console.WriteLine("Driving");
    }
}

public class Car : Vehicle // Inherits from Vehicle
{
    public int Wheels = 4;
}

class Program
{
    static void Main(string[] args)
    {
        Car myCar = new Car();
        myCar.Drive(); // Use inherited method
        Console.WriteLine($"License: {myCar.LicensePlate}, Wheels: {myCar.Wheels}");
    }
}

3. How does access modification affect inheritance in C#?

Answer: Access modifiers such as public, protected, internal, protected internal, and private dictate the visibility and accessibility of class members in the context of inheritance.

Key Points:
- public members are accessible from any part of the program.
- protected members are accessible within the class itself and by derived class instances.
- private members are only accessible within the class it is declared and are not inherited.
- internal members are accessible within the same assembly, but not from another assembly.
- protected internal members are accessible within the same assembly or from derived classes in other assemblies.

Example:

public class BaseClass
{
    public int publicField = 1;
    protected int protectedField = 2;
    private int privateField = 3;
}

public class DerivedClass : BaseClass
{
    public void PrintFields()
    {
        Console.WriteLine(publicField); // Accessible
        Console.WriteLine(protectedField); // Accessible
        // Console.WriteLine(privateField); // Not accessible, results in a compile-time error
    }
}

4. Explain how polymorphism works in the context of inheritance in C#.

Answer: Polymorphism in the context of inheritance allows derived classes to override or extend the functionality of base class methods. This enables multiple derived classes to share the same interface or method signature, but each can provide its own implementation.

Key Points:
- Polymorphism is achieved through method overriding using the virtual keyword in the base class method and the override keyword in the derived class method.
- The base keyword can be used to call the base class version of an overridden method.
- It is a key feature of object-oriented programming that helps in making the software more modular and extensible.

Example:

public class Animal
{
    public virtual void Speak() // Virtual method
    {
        Console.WriteLine("Some sound");
    }
}

public class Dog : Animal
{
    public override void Speak() // Overriding method
    {
        Console.WriteLine("Woof");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal myAnimal = new Animal();
        myAnimal.Speak();

        Dog myDog = new Dog();
        myDog.Speak(); // Calls the overridden method in the Dog class
    }
}