Object-Oriented Programming (OOP) in C#

Object-Oriented Programming (OOP) in C# 

Object-Oriented Programming (OOP) in C# is a programming paradigm that uses objects to design and build applications. It revolves around the following four fundamental principles:

  • Encapsulation

Encapsulation is the practice of bundling the data (fields) and methods (functions) that operate on the data into a single unit, called a class. It also involves restricting direct access to some components using access modifiers like private, protected, public, and internal.

  • Inheritance

Inheritance allows a class (child class) to inherit fields and methods from another class (parent class). It promotes code reuse and establishes an "is-a" relationship.

  • Polymorphism

Polymorphism enables one interface to be used for different types. It can be achieved through 

  • method overriding (runtime polymorphism) 
  • method overloading (compile-time polymorphism).

Method Overloading:

public class Calculator 
    { public int Add(int a, int b) 
         { return a + b; } 

public double Add(double a, double b) 
     { return a + b; } 
}

Method Overriding:

public class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("The animal speaks.");
    }
}

public class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("The dog barks.");
    }
}

  • Abstraction

Abstraction focuses on hiding complex implementation details and exposing only the essential features. It is achieved using abstract classes and interfaces.

What is Object-Oriented Programming (OOP)? Explain its principles.

OOP is a programming paradigm based on the concept of "objects," which can contain data in the form of fields (properties) and code in the form of methods.

The four main principles of OOP are:

  1. Encapsulation: Bundling data and methods into a class and restricting access using access modifiers.
  2. Inheritance: Allowing a class to inherit properties and methods from another class.
  3. Polymorphism: Using a single interface to represent different underlying forms (e.g., method overriding and overloading).
  4. Abstraction: Hiding implementation details and exposing only the essential features.

What is the difference between a class and an object?

  • A class is a blueprint or template that defines the structure and behavior of objects. Example: class Car { }
  • An object is an instance of a class that is created in memory. Example: Car myCar = new Car();

What is the difference between method overloading and method overriding?



AspectMethod OverloadingMethod Overriding
DefinitionMultiple methods in the same class with the same name but different parameters.A derived class redefines a method of its base class.
Compile-time vs RuntimeCompile-time polymorphism.Runtime polymorphism.
Access ModifierMethods can have any access modifier.The overriding method must be public, protected, or less restrictive.
Examplevoid Add(int a, int b)
void Add(double a, double b)
virtual void Display()
override void Display()

What is an abstract class? How is it different from an interface?

An abstract class is a class that cannot be instantiated and may contain abstract methods (without implementation) and concrete methods (with implementation).

Differences:

AspectAbstract ClassInterface
ImplementationCan have concrete methods with implementation.Only method signatures (until C# 8.0, now it can have default methods).
InheritanceSupports single inheritance.Supports multiple inheritance.
Access ModifiersCan have fields with access modifiers.Cannot have fields; all members are public by default.
Exampleabstract class Shape { abstract void Draw(); }interface IShape { void Draw(); }

What is the difference between virtual, override, and new keywords in C#?

  • virtual: Used in the base class to indicate that a method can be overridden in a derived class.
  • override: Used in the derived class to provide a new implementation for a virtual or abstract method in the base class.
  • new: Used in the derived class to hide a method from the base class if it is not virtual.

What is a sealed class?

A sealed class is a class that cannot be inherited. It is declared using the sealed keyword. It is used to prevent further derivation for security or design purposes.


What is the difference between encapsulation and abstraction?

Aspect

Encapsulation

Abstraction

Definition

Hides the internal state of an object and provides controlled access using access modifiers.

Hides implementation details and shows only essential features.

Implementation

Achieved through classes, properties, and access modifiers (private, public).

Achieved through abstract classes and interfaces.

Example

private int age; public int Age { get; set; }

abstract class Shape { abstract void Draw(); }

What is the difference between this and base keywords in C#?

  • this: Refers to the current instance of the class.
  • base: Refers to the parent class of the current class and is used to access base class members.
class Parent
{
    public void Display() { Console.WriteLine("Parent Display"); }
}

class Child : Parent
{
    public void Show()
    {
        base.Display(); // Calls Parent's Display
        Console.WriteLine("Child Show");
    }
}


Can you explain access modifiers in C#?

C# provides the following access modifiers to control the visibility of class members:

  • public: Accessible from anywhere.
  • private: Accessible only within the containing class.
  • protected: Accessible within the containing class and derived classes.
  • internal: Accessible within the same assembly.
  • protected internal: Accessible within the same assembly or derived classes.
  • private protected: Accessible within the containing class or derived classes in the same assembly.

What is the difference between shallow copy and deep copy?

Answer:

  • Shallow Copy: Copies the reference of the objects. Changes in the copied object affect the original object.
  • Deep Copy: Copies the entire object along with its dependencies. Changes in the copied object do not affect the original object.

Example of Shallow Copy:

Person person1 = new Person { Name = "John" }; Person person2 = person1; // Both refer to the same object

Deep Copy:

Person person1 = new Person { Name = "John" }; Person person2 = new Person { Name = person1.Name }; // Creates a new object

Comments

Popular posts from this blog

SQL

C# operations

.NET Web API interview questions