techmore.in

C# - OOP

Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to structure and organize code. C# is a fully object-oriented language that provides robust support for OOP principles. Here's a detailed overview of OOP concepts in C#:

1. Core Principles of OOP

  1. Encapsulation: Bundling data and methods that operate on the data into a single unit (class) and restricting access to some of the object's components. This helps to protect the internal state of an object from unintended modification.

  2. Inheritance: Allows a class (derived or child class) to inherit fields, properties, and methods from another class (base or parent class). This promotes code reusability and establishes a natural hierarchy.

  3. Polymorphism: Enables a method or property to behave differently based on the object it is acting upon. This can be achieved through method overriding or method overloading.

  4. Abstraction: Hides complex implementation details and exposes only the necessary features of an object. This is often achieved through abstract classes and interfaces.

2. Encapsulation

Encapsulation is about bundling the data (fields) and methods (functions) that operate on the data into a single class and restricting access to some of the object's components.

Example:

csharp
public class BankAccount { private decimal balance; // Private field public void Deposit(decimal amount) // Public method { if (amount > 0) { balance += amount; } } public void Withdraw(decimal amount) // Public method { if (amount > 0 && amount <= balance) { balance -= amount; } } public decimal GetBalance() // Public method { return balance; } }

3. Inheritance

Inheritance allows one class to inherit the fields, properties, and methods of another class. The derived class (child class) can extend or override the functionality of the base class (parent class).

Example:

csharp
public class Animal { public void Eat() { Console.WriteLine("Eating..."); } } public class Dog : Animal { public void Bark() { Console.WriteLine("Woof!"); } } Dog dog = new Dog(); dog.Eat(); // Inherited method dog.Bark(); // Derived class method

4. Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common base class. It is achieved through method overriding (runtime polymorphism) and method overloading (compile-time polymorphism).

Method Overriding (Runtime Polymorphism):

csharp
public class Animal { public virtual void MakeSound() { Console.WriteLine("Animal sound"); } } public class Dog : Animal { public override void MakeSound() { Console.WriteLine("Woof!"); } } Animal animal = new Dog(); animal.MakeSound(); // Outputs: Woof!

Method Overloading (Compile-time Polymorphism):

csharp
public class Printer { public void Print(string message) { Console.WriteLine(message); } public void Print(int number) { Console.WriteLine(number); } } Printer printer = new Printer(); printer.Print("Hello"); // Outputs: Hello printer.Print(123); // Outputs: 123

5. Abstraction

Abstraction involves creating abstract classes and interfaces to define common features without implementing them. This allows derived classes to provide specific implementations.

Abstract Classes:

An abstract class cannot be instantiated and can contain abstract methods that must be implemented by derived classes.

csharp
public abstract class Shape { public abstract double GetArea(); // Abstract method public void Display() { Console.WriteLine($"The area is {GetArea()}"); } } public class Circle : Shape { public double Radius { get; set; } public override double GetArea() { return Math.PI * Radius * Radius; } }

Interfaces:

An interface defines a contract that implementing classes must follow. Interfaces cannot contain implementation, only method signatures and properties.

csharp
public interface IShape { double GetArea(); } public class Rectangle : IShape { public double Width { get; set; } public double Height { get; set; } public double GetArea() { return Width * Height; } }

6. Access Modifiers

Access modifiers control the visibility of class members. They include:

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

Example:

csharp
public class MyClass { private int privateField; protected int protectedField; public int publicField; internal int internalField; }

7. Constructors and Destructors

  • Constructors are special methods used to initialize objects. They have the same name as the class and do not have a return type.

  • Destructors (finalizers) are used to clean up resources before an object is destroyed. They have the same name as the class but are prefixed with a tilde (~).

Example:

csharp
public class Example { public Example() // Constructor { Console.WriteLine("Object created"); } ~Example() // Destructor { Console.WriteLine("Object destroyed"); } }

8. Static Classes and Members

  • Static Classes: Cannot be instantiated and can only contain static members. They are used for utility or helper functions.

  • Static Members: Belong to the class itself rather than any instance.

Example:

csharp
public static class MathUtils { public static int Add(int a, int b) { return a + b; } } int result = MathUtils.Add(3, 4); // Outputs: 7

Summary

Object-Oriented Programming (OOP) in C# is centered around the concepts of encapsulation, inheritance, polymorphism, and abstraction. These principles help in designing systems that are modular, reusable, and easy to maintain. By understanding and effectively using classes, objects, methods, inheritance, polymorphism, and abstraction, you can build robust and scalable applications in C#.