C# Interview Questions
C# Programming
Web DevelopmentFrontendBackendGame DevQuestion 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
-
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.
-
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 }
- The derived class specifies the base class it inherits from using a colon (
-
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.
-
Member Inheritance:
- The derived class inherits all non-private members of the base class, including fields, properties, methods, and events.
-
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"); } }
- The derived class can provide a specific implementation of a method that is already defined in the base class. This is done using the
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
-
Base Class (
Animal
):- Contains properties (
Name
), methods (Eat
), and a virtual method (MakeSound
).
- Contains properties (
-
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 forDog
.
- Inherits properties and methods from
-
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 theDog
class overrides theAnimal
class implementation.
- An instance of the
Benefits of Inheritance
-
Code Reusability:
- Promotes code reuse by allowing new classes to inherit and extend existing functionality without rewriting code.
-
Extensibility:
- Makes it easier to extend existing classes with new features, improving maintainability and scalability.
-
Hierarchical Relationships:
- Models real-world relationships and hierarchies naturally, making the code more intuitive and logical.
-
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.