OOP Interview Questions

Created
Aug 22, 2024 02:41 AM

Fundamentals

Object-Oriented Programming

Object-Oriented Programming (OOP) is a fundamental concept in C# that allows developers to create complex and maintainable applications. C# is a multi-paradigm programming language that supports OOP principles such as encapsulation, inheritance, and polymorphism. Here's an overview of OOP in C#:

Classes and Objects

In C#, a class is a blueprint for creating objects. It encapsulates data (fields) and methods that operate on that data. An object is an instance of a class.
class Car { string make; string model; public void Start() { Console.WriteLine("Starting the car."); } } Car myCar = new Car(); myCar.Start();

Types of OOP

Encapsulation

Encapsulation is the mechanism that binds together code and data into a single unit (class) and keeps both safe from outside interference and misuse. Access modifiers like public, private, and protected control the visibility of class members.
class BankAccount { private double balance; public void Deposit(double amount) { balance += amount; } public void Withdraw(double amount) { if (balance >= amount) { balance -= amount; } else { Console.WriteLine("Insufficient funds."); } } }

Inheritance (HAS-A relationship)

Inheritance allows creating new classes based on existing ones. The new class (derived class) inherits fields and methods from the base class.
class ElectricCar : Car { int batteryLevel; public void Charge() { Console.WriteLine("Charging the electric car."); } }

Polymorphism(IS-A relationship)

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables a single interface to represent different underlying forms.
class Program { static void Main() { Car car1 = new Car(); Car car2 = new ElectricCar(); car1.Start(); // Outputs: Starting the car. car2.Start(); // Outputs: Starting the car. } }

Types of Polymorphism

There are two primary types of polymorphism in C#:
  1. Compile-Time Polymorphism (Static Polymorphism)
  1. Run-Time Polymorphism (Dynamic Polymorphism)

1. Compile-Time Polymorphism (Static Polymorphism)

Compile-time polymorphism is resolved during the compilation of the program. It is achieved through:
  • Method Overloading: This occurs when multiple methods in the same class have the same name but different parameters (type, number, or both). The compiler determines which method to call based on the method signature at compile time.
  • Example of Method Overloading:
    • public class Calculator { public int Add(int a, int b) { return a + b; } public int Add(int a, int b, int c) { return a + b + c; } } class Program { static void Main(string[] args) { Calculator calc = new Calculator(); Console.WriteLine(calc.Add(5, 10)); // Calls the first Add method Console.WriteLine(calc.Add(5, 10, 15)); // Calls the second Add method } }
  • Operator Overloading: This allows developers to define custom behavior for operators (like +, -, etc.) when applied to user-defined types.
  • Example of Operator Overloading:
    • public class Complex { public double Real { get; set; } public double Imaginary { get; set; } public static Complex operator +(Complex c1, Complex c2) { return new Complex { Real = c1.Real + c2.Real, Imaginary = c1.Imaginary + c2.Imaginary }; } }

2. Run-Time Polymorphism (Dynamic Polymorphism)

Run-time polymorphism is resolved during the execution of the program. It is achieved through:
  • Method Overriding: This occurs when a derived class provides a specific implementation of a method that is already defined in its base class. The base class method must be marked as virtual, and the derived class method must use the override keyword.
  • Example of Method Overriding:
    • public class Animal { public virtual void Speak() { Console.WriteLine("Animal speaks"); } } public class Dog : Animal { public override void Speak() { Console.WriteLine("Dog barks"); } } public class Cat : Animal { public override void Speak() { Console.WriteLine("Cat meows"); } } class Program { static void Main(string[] args) { Animal myAnimal; myAnimal = new Dog(); myAnimal.Speak(); // Outputs: Dog barks myAnimal = new Cat(); myAnimal.Speak(); // Outputs: Cat meows } }

Abstract Classes and Interfaces

Abstract classes and interfaces provide a way to define common functionality and enforce contracts.
abstract class Vehicle { public abstract void Start(); } interface IElectric { void Charge(); } class Tesla : Vehicle, IElectric { public override void Start() { Console.WriteLine("Starting the Tesla."); } public void Charge() { Console.WriteLine("Charging the Tesla."); } }

Benefits of OOP in C#

  • Modularity: OOP allows breaking a program into smaller, more manageable modules (classes).
  • Code Reuse: Inheritance enables reusing existing code, reducing development time and effort.
  • Extensibility: OOP makes it easier to add new features to an application.
  • Maintainability: OOP promotes better code organization and readability, making it easier to maintain and update.

QnA

How can you implement multiple inheritance in C#?

Using Interfaces, you can implement multiple inheritance in C#.

Are private class members inherited from the derived class?

  • Yes, the private members are also inherited in the derived class but we will not be able to access them.
  • Trying to access a private base class member in the derived class will report a compile-time error.