C# Interview Questions

36 Questions
C# Programming

C# Programming

Web DevelopmentFrontendBackendGame Dev

Question 7

What is inheritance in C#?

Answer:

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and methods from another class. In C#, inheritance enables the creation of a new class (called a derived class or subclass) that is based on an existing class (called a base class or superclass). The derived class inherits the fields, properties, and methods of the base class, and can also have additional members or override existing ones.

Key Concepts of Inheritance

  1. Base Class and Derived Class:

    • Base Class: The class whose members (fields, properties, methods) are inherited. Also known as the parent class or superclass.
    • Derived Class: The class that inherits from the base class. Also known as the child class or subclass.
  2. Syntax:

    • The derived class specifies the base class it inherits from using a colon (:) followed by the base class name.
    public class BaseClass
    {
        // Base class members
    }
    
    public class DerivedClass : BaseClass
    {
        // Derived class members
    }
  3. Access Modifiers:

    • The access level of the base class members determines whether they are accessible in the derived class. Public and protected members are accessible, while private members are not directly accessible.
  4. Member Inheritance:

    • The derived class inherits all non-private members of the base class, including fields, properties, methods, and events.
  5. Method Overriding:

    • The derived class can provide a specific implementation of a method that is already defined in the base class. This is done using the override keyword.
    public class BaseClass
    {
        public virtual void Display()
        {
            Console.WriteLine("Display from BaseClass");
        }
    }
    
    public class DerivedClass : BaseClass
    {
        public override void Display()
        {
            Console.WriteLine("Display from DerivedClass");
        }
    }

Example of Inheritance in C#

Here’s a practical example to illustrate inheritance in C#:

using System;

public class Animal
{
    public string Name { get; set; }

    public void Eat()
    {
        Console.WriteLine($"{Name} is eating.");
    }

    public virtual void MakeSound()
    {
        Console.WriteLine("Animal makes a sound.");
    }
}

public class Dog : Animal
{
    public string Breed { get; set; }

    public override void MakeSound()
    {
        Console.WriteLine($"{Name}, the dog, barks.");
    }

    public void Fetch()
    {
        Console.WriteLine($"{Name} is fetching the ball.");
    }
}

public class Program
{
    public static void Main()
    {
        Dog myDog = new Dog
        {
            Name = "Buddy",
            Breed = "Golden Retriever"
        };

        myDog.Eat();        // Inherited from Animal class
        myDog.MakeSound();  // Overridden in Dog class
        myDog.Fetch();      // Defined in Dog class

        Console.WriteLine($"{myDog.Name} is a {myDog.Breed}.");
    }
}

Key Points in the Example

  1. Base Class (Animal):

    • Contains properties (Name), methods (Eat), and a virtual method (MakeSound).
  2. Derived Class (Dog):

    • Inherits properties and methods from Animal.
    • Adds a new property (Breed) and a new method (Fetch).
    • Overrides the MakeSound method to provide a specific implementation for Dog.
  3. Using the Derived Class:

    • An instance of the Dog class is created, which can use both inherited and its own members.
    • The MakeSound method of the Dog class overrides the Animal class implementation.

Benefits of Inheritance

  1. Code Reusability:

    • Promotes code reuse by allowing new classes to inherit and extend existing functionality without rewriting code.
  2. Extensibility:

    • Makes it easier to extend existing classes with new features, improving maintainability and scalability.
  3. Hierarchical Relationships:

    • Models real-world relationships and hierarchies naturally, making the code more intuitive and logical.
  4. Polymorphism:

    • Supports polymorphism, allowing a base class reference to point to derived class objects and call overridden methods dynamically at runtime.

Inheritance is a powerful feature in C# that helps in building a modular, reusable, and maintainable codebase by establishing a clear hierarchical relationship between classes.

Recent job openings