C# Interview Questions
C# Programming
Web DevelopmentFrontendBackendGame DevQuestion 6
Explain the four pillars of object-oriented programming.
Answer:
Object-oriented programming (OOP) is a programming paradigm that uses objects and classes to structure software in a way that models real-world entities and relationships. The four fundamental principles, or "pillars," of OOP are encapsulation, abstraction, inheritance, and polymorphism. These principles help create a modular, reusable, and maintainable codebase. Hereβs an explanation of each pillar:
1. Encapsulation
Definition:
Encapsulation is the principle of bundling data (attributes) and methods (functions) that operate on the data into a single unit called a class. It restricts direct access to some of the object's components, which can help prevent the accidental modification of data.
Benefits:
- Data Protection: Protects the internal state of the object from unintended or harmful modifications.
- Modularity: Enhances modularity by organizing code into discrete, self-contained units.
- Maintenance: Makes maintenance easier by isolating changes to the internal implementation.
Example:
public class Person
{
private string name;
private int age;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set
{
if (value >= 0)
{
age = value;
}
}
}
}
In this example, the name
and age
fields are encapsulated within the Person
class and can only be accessed through the public properties Name
and Age
.
2. Abstraction
Definition:
Abstraction is the principle of simplifying complex reality by modeling classes appropriate to the problem, and working at the most relevant level of inheritance for a particular aspect of the problem.
Benefits:
- Simplification: Reduces complexity by hiding irrelevant details and showing only the necessary features of an object.
- Focus: Allows developers to focus on interactions at a higher level without needing to understand all underlying details.
Example:
public abstract class Animal
{
public abstract void MakeSound();
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Bark");
}
}
public class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("Meow");
}
}
Here, Animal
is an abstract class that defines an abstract method MakeSound
. The Dog
and Cat
classes implement this method, providing specific sounds for each animal.
3. Inheritance
Definition:
Inheritance is the principle where a new class (derived class) inherits the properties and behavior (methods) of an existing class (base class). This promotes code reuse and establishes a natural hierarchical relationship between classes.
Benefits:
- Reusability: Promotes code reuse by allowing new classes to reuse existing code.
- Hierarchy: Establishes a natural hierarchy and relationships between classes.
- Maintenance: Simplifies maintenance by enabling changes in the base class to propagate to derived classes.
Example:
public class Vehicle
{
public string Make { get; set; }
public string Model { get; set; }
public void StartEngine()
{
Console.WriteLine("Engine started");
}
}
public class Car : Vehicle
{
public int NumberOfDoors { get; set; }
}
public class Motorcycle : Vehicle
{
public bool HasSidecar { get; set; }
}
In this example, Car
and Motorcycle
classes inherit from the Vehicle
class, reusing its properties and methods.
4. Polymorphism
Definition:
Polymorphism is the principle that allows objects of different classes to be treated as objects of a common base class. It enables one interface to be used for a general class of actions, with specific behavior determined by the exact nature of the situation.
Benefits:
- Flexibility: Increases flexibility and maintainability of code by allowing the same interface to represent different underlying forms (data types).
- Extensibility: Makes it easier to extend systems with new functionalities.
Example:
public class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a shape");
}
}
public class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a circle");
}
}
public class Rectangle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a rectangle");
}
}
public class Program
{
public static void Main()
{
Shape[] shapes = new Shape[2];
shapes[0] = new Circle();
shapes[1] = new Rectangle();
foreach (Shape shape in shapes)
{
shape.Draw();
}
}
}
In this example, the Draw
method is polymorphic. It can be called on any object of type Shape
, and the specific implementation depends on the actual derived type of the object (Circle
or Rectangle
).
Summary
- Encapsulation: Bundles data and methods, protects object integrity.
- Abstraction: Simplifies complex reality, focuses on relevant details.
- Inheritance: Reuses code, establishes hierarchies.
- Polymorphism: Treats objects uniformly, increases flexibility and extensibility.
These four pillars work together to provide a powerful, flexible, and modular approach to software development in object-oriented programming.