1. Can you explain the concept of object-oriented programming (OOP)?

Basic

1. Can you explain the concept of object-oriented programming (OOP)?

Overview

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data in the form of fields (often known as attributes or properties) and code in the form of procedures (often known as methods). OOP is significant in software engineering for its ability to enhance software design, make code more reusable, and make it easier to maintain.

Key Concepts

  1. Encapsulation: Bundling of data with the methods that operate on that data.
  2. Inheritance: Mechanism wherein a new class is derived from an existing class.
  3. Polymorphism: Ability of a single interface to represent different underlying forms (data types).

Common Interview Questions

Basic Level

  1. What is Object-Oriented Programming (OOP)?
  2. Can you explain the concept of Encapsulation with an example?

Intermediate Level

  1. How does Inheritance work in OOP, and can you provide a C# example?

Advanced Level

  1. Explain Polymorphism in C# with an example, covering both static and dynamic polymorphism.

Detailed Answers

1. What is Object-Oriented Programming (OOP)?

Answer: Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to design applications and computer programs. It utilizes several core concepts such as encapsulation, inheritance, and polymorphism to increase code reusability, scalability, and maintainability.

Key Points:
- Abstraction: Simplifying complex reality by modeling classes appropriate to the problem.
- Encapsulation: Hiding the internal state of an object and requiring all interaction to be performed through an object's methods.
- Inheritance: Allowing a new class to inherit properties and methods of an existing class.
- Polymorphism: Enabling a single interface to represent different data types.

Example:

public class Animal  // Base class (parent)
{
    public virtual void animalSound()  // Method in the base class
    {
        Console.WriteLine("The animal makes a sound");
    }
}

public class Pig : Animal  // Derived class (child)
{
    public override void animalSound()
    {
        // The body of animalSound() is provided here
        Console.WriteLine("The pig says: wee wee");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Animal myAnimal = new Animal();  // Create a Animal object
        Animal myPig = new Pig();        // Create a Pig object
        myAnimal.animalSound();          // Call the method in Animal
        myPig.animalSound();             // Call the method in Pig
    }
}

2. Can you explain the concept of Encapsulation with an example?

Answer: Encapsulation is one of the fundamental concepts in object-oriented programming. It is the technique of bundling the data (attributes) and methods (functions) that operate on the data into a single unit, or class, and restricting access to the details of the implementation of the class.

Key Points:
- Encapsulation helps to protect an object’s internal state from outside modification.
- It allows the class to control the values stored in its fields.
- The class interface can remain constant while the implementation can change.

Example:

public class Account
{
    private double balance;  // Private field

    public void Deposit(double amount)  // Public method to modify the private field
    {
        if (amount > 0)
        {
            balance += amount;
        }
    }

    public double GetBalance()  // Public method to access the private field
    {
        return balance;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Account myAccount = new Account();
        myAccount.Deposit(100);
        Console.WriteLine($"Balance: {myAccount.GetBalance()}");
    }
}

3. How does Inheritance work in OOP, and can you provide a C# example?

Answer: Inheritance allows a class (derived class) to inherit fields and methods from another class (base class). It supports the concept of "is-a" relationship. Through inheritance, we can create a general class that defines traits common to a set of related items.

Key Points:
- It enables code reusability and establishes a relationship between different classes.
- Derived classes can override or extend the functionality of base classes.
- Base class methods marked with virtual can be overridden in derived classes using override.

Example:

public class Vehicle  // Base class
{
    public string brand = "Ford";  
    public void honk()             
    {
        Console.WriteLine("Tuut, tuut!");
    }
}

public class Car : Vehicle  // Derived class
{
    public string modelName = "Mustang";  
}

class Program
{
    static void Main(string[] args)
    {
        Car myCar = new Car();
        myCar.honk();
        Console.WriteLine($"{myCar.brand} {myCar.modelName}");
    }
}

4. Explain Polymorphism in C# with an example, covering both static and dynamic polymorphism.

Answer: Polymorphism in C# allows methods to do different things based on the object it is acting upon. In simple terms, it allows one interface to be used for a general class of actions. There are two types: static (compile-time) and dynamic (run-time) polymorphism.

Key Points:
- Static Polymorphism: Achieved using method overloading.
- Dynamic Polymorphism: Achieved using method overriding.

Example:

// Static Polymorphism Example - Method Overloading
public class Print
{
    public void display(int i)
    {
        Console.WriteLine($"Printing int: {i}");
    }
    public void display(double f)
    {
        Console.WriteLine($"Printing double: {f}");
    }
}

// Dynamic Polymorphism Example - Method Overriding
public class Animal
{
    public virtual void animalSound()
    {
        Console.WriteLine("The animal makes a sound");
    }
}

public class Pig : Animal
{
    public override void animalSound()
    {
        Console.WriteLine("The pig says: wee wee");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Print print = new Print();
        print.display(5);
        print.display(500.263);

        Animal myAnimal = new Animal();
        Animal myPig = new Pig();

        myAnimal.animalSound();
        myPig.animalSound();
    }
}

This demonstrates both static and dynamic polymorphism through method overloading and overriding, illustrating how the same method name can perform different tasks based on the input parameters or the derived class implementation.