Object-Oriented Programming (OOP) in C# is a paradigm that uses objects and classes to structure software programs. It is based on four key principles: Encapsulation, Inheritance, Polymorphism, and Abstraction. Here's a detailed explanation of each concept with examples:
1. Encapsulation
Encapsulation is the mechanism of hiding the internal details of an object and exposing only the necessary components. It helps in protecting the data and methods from outside interference and misuse.
Example:
csharp
public class BankAccount
{
private decimal balance;
public void Deposit(decimal amount)
{
if (amount > 0)
{
balance += amount;
}
}
public decimal GetBalance()
{
return balance;
}
}
public class Program
{
public static void Main()
{
BankAccount account = new BankAccount();
account.Deposit(100);
Console.WriteLine("Balance: " + account.GetBalance());
}
}
2. Inheritance
Inheritance allows a class to inherit properties and methods from another class. The class that inherits is called the derived class (or child class), and the class being inherited from is the base class (or parent class).
Example:
csharp
public class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Barking...");
}
}
public class Program
{
public static void Main()
{
Dog dog = new Dog();
dog.Eat();
dog.Bark();
}
}
3. Polymorphism
Polymorphism allows methods to do different things based on the object it is acting upon. It can be achieved through method overriding and method overloading.
Example: Method Overriding:
csharp
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Animal sound");
}
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Woof");
}
}
public class Program
{
public static void Main()
{
Animal myDog = new Dog();
myDog.MakeSound();
}
}
Example: Method Overloading:
csharp
public class MathOperations
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
}
public class Program
{
public static void Main()
{
MathOperations math = new MathOperations();
Console.WriteLine(math.Add(5, 3));
Console.WriteLine(math.Add(5.5, 3.3));
}
}
4. Abstraction
Abstraction involves hiding the complex implementation details and showing only the essential features of the object. It is achieved using abstract classes and interfaces.
Example: Using Abstract Class:
csharp
public abstract class Shape
{
public abstract double CalculateArea();
}
public class Circle : Shape
{
public double Radius { get; set; }
public override double CalculateArea()
{
return Math.PI * Radius * Radius;
}
}
public class Program
{
public static void Main()
{
Circle circle = new Circle { Radius = 5 };
Console.WriteLine("Area of Circle: " + circle.CalculateArea());
}
}
Example: Using Interface:
csharp
public interface IShape
{
double CalculateArea();
}
public class Rectangle : IShape
{
public double Width { get; set; }
public double Height { get; set; }
public double CalculateArea()
{
return Width * Height;
}
}
public class Program
{
public static void Main()
{
Rectangle rectangle = new Rectangle { Width = 5, Height = 4 };
Console.WriteLine("Area of Rectangle: " + rectangle.CalculateArea());
}
}
These concepts form the foundation of object-oriented programming in C#. They help in creating robust, reusable, and maintainable code. If you have any more questions or need further examples, feel free to ask! ??