Overview
Inheritance and polymorphism are fundamental concepts of object-oriented programming (OOP) that allow for the creation of hierarchical class structures and the ability to use objects of one class where objects of another class are expected, respectively. In C#, implementing inheritance and polymorphism simplifies code maintenance, enhances code reusability, and allows for dynamic method binding, making it crucial for designing robust and scalable applications.
Key Concepts
- Inheritance - Enables a class to inherit fields and methods from another class.
- Polymorphism - Allows objects of different classes to be treated as objects of a common superclass.
- Abstract Classes and Interfaces - Serve as a foundation for defining abstract methods and properties that derived classes must implement, showcasing polymorphism.
Common Interview Questions
Basic Level
- How do you define a base (parent) class and a derived (child) class in C#?
- Can you give an example of method overriding in C#?
Intermediate Level
- How do interfaces contribute to polymorphism in C#?
Advanced Level
- Discuss the use of abstract classes versus interfaces in designing a polymorphic system in C#.
Detailed Answers
1. How do you define a base (parent) class and a derived (child) class in C#?
Answer: In C#, a base class is defined as a normal class, and a derived class is defined by specifying a base class from which it inherits using the :
symbol. The derived class inherits all public and protected members of the base class.
Key Points:
- The derived class can add its own members.
- The derived class can override inherited members.
- Base class constructors are not inherited but can be called explicitly from the derived class constructor.
Example:
public class Animal // Base class
{
public string Name { get; set; }
public void Eat()
{
Console.WriteLine($"{Name} is eating.");
}
}
public class Dog : Animal // Derived class
{
public Dog(string name)
{
Name = name;
}
public void Bark()
{
Console.WriteLine($"{Name} says Woof!");
}
}
2. Can you give an example of method overriding in C#?
Answer: Method overriding in C# allows a derived class to provide a specific implementation of a method that is already defined in its base class. This is achieved using the override
keyword in the derived class and marking the method in the base class with the virtual
or abstract
keyword.
Key Points:
- The method signature must match between the base and derived class.
- The virtual
keyword allows the base class method to be overridden.
- The override
keyword is used in the derived class to replace the base class method.
Example:
public class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a shape.");
}
}
public class Circle : Shape
{
public override void Draw()
{
// Custom implementation for Circle
Console.WriteLine("Drawing a circle.");
}
}
3. How do interfaces contribute to polymorphism in C#?
Answer: Interfaces in C# define a contract that classes can implement, specifying what methods or properties a class must have without defining how these methods or properties are implemented. This allows objects of different classes that implement the same interface to be treated as objects of the interface type, facilitating polymorphism.
Key Points:
- Interfaces contain only the declaration of methods, properties, events, or indexers.
- A class can implement multiple interfaces, supporting multiple forms of polymorphism.
- Interfaces are crucial for dependency injection and testing frameworks.
Example:
public interface IDrawable
{
void Draw();
}
public class Rectangle : IDrawable
{
public void Draw()
{
Console.WriteLine("Drawing a rectangle.");
}
}
public class Triangle : IDrawable
{
public void Draw()
{
Console.WriteLine("Drawing a triangle.");
}
}
4. Discuss the use of abstract classes versus interfaces in designing a polymorphic system in C#.
Answer: Abstract classes and interfaces in C# are both used to define a contract for classes, but they serve different purposes and have different constraints. Abstract classes can provide some implementation (method bodies), whereas interfaces cannot. Interfaces allow a class to inherit multiple contracts, facilitating a form of multiple inheritance.
Key Points:
- Abstract classes are best when classes share a common implementation.
- Interfaces are ideal for defining a common API that multiple unrelated classes can implement.
- C# 8.0 introduced default interface methods, blurring the lines between interfaces and abstract classes but still not allowing state (fields).
Example:
public abstract class Animal
{
public abstract void Eat();
public void Sleep()
{
Console.WriteLine("Sleeping");
}
}
public interface IMovable
{
void Move();
}
// Inherits abstract class and implements interface
public class Human : Animal, IMovable
{
public override void Eat()
{
Console.WriteLine("Human is eating.");
}
public void Move()
{
Console.WriteLine("Human is walking.");
}
}
This approach allows for a flexible design, enabling classes to share common behavior through abstract classes while also adhering to specific contracts through interfaces, leveraging the strengths of both inheritance and polymorphism.