Object-Oriented Programming (OOP) in .NET Core
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).
1. Classes and Objects
A class is a blueprint for creating objects, providing initial values for its state (member variables or attributes) and implementations of its behavior (member functions or methods). An object is an instance of a class.
public class Car {
public string Make;
public string Model;
public int Year;
public void StartEngine() {
Console.WriteLine("Engine started!");
}
}
// Creating an object (instance) of the Car class
Car myCar = new Car();
myCar.Make = "Toyota";
myCar.Model = "Camry";
myCar.Year = 2023;
myCar.StartEngine();
2. Encapsulation
Encapsulation is the bundling of data (attributes) and methods that operate on the data within a single unit, called a class. It also involves restricting direct access to some of an object's components, which is achieved by making the attributes private and providing public getter and setter methods.
public class BankAccount {
private decimal balance;
public decimal Balance {
get { return balance; }
private set { balance = value; } // Only set internally
}
public void Deposit(decimal amount) {
if (amount > 0) {
Balance += amount;
Console.WriteLine($"Deposited: {amount:C}");
}
}
public bool Withdraw(decimal amount) {
if (amount > 0 && Balance >= amount) {
Balance -= amount;
Console.WriteLine($"Withdrew: {amount:C}");
return true;
}
Console.WriteLine("Withdrawal failed.");
return false;
}
}
3. Inheritance
Inheritance is a mechanism in which one class acquires the properties and behaviors of another class. The class that inherits is called the derived class (or child class), and the class from which it inherits is called the base class (or parent class).
public class Vehicle {
public string Brand;
public void Drive() {
Console.WriteLine("Driving...");
}
}
public class ElectricCar : Vehicle {
public int BatteryCapacity;
public void Charge() {
Console.WriteLine("Charging...");
}
}
// Usage
ElectricCar myElectricCar = new ElectricCar();
myElectricCar.Brand = "Tesla";
myElectricCar.BatteryCapacity = 100;
myElectricCar.Drive(); // Inherited method
myElectricCar.Charge(); // Own method
4. Polymorphism
Polymorphism, which means "many forms", allows objects of different classes to be treated as objects of a common base class. This can be achieved through method overriding (runtime polymorphism) and method overloading (compile-time polymorphism).
// Base class
public class Animal {
public virtual void MakeSound() {
Console.WriteLine("Some generic animal sound.");
}
}
// Derived classes
public class Dog : Animal {
public override void MakeSound() {
Console.WriteLine("Woof!");
}
}
public class Cat : Animal {
public override void MakeSound() {
Console.WriteLine("Meow!");
}
}
// Usage
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.MakeSound(); // Outputs: Woof!
myCat.MakeSound(); // Outputs: Meow!
5. Abstraction
Abstraction is the process of hiding the implementation details and showing only the essential features of the object. It is achieved by using abstract classes and interfaces. An abstract class cannot be instantiated and may contain abstract methods (methods without an implementation).
// Abstract class
public abstract class Shape {
public abstract double GetArea();
public void Display() {
Console.WriteLine("This is a shape.");
}
}
// Concrete class implementing abstraction
public class Circle : Shape {
public double Radius { get; set; }
public Circle(double radius) {
Radius = radius;
}
public override double GetArea() {
return Math.PI * Radius * Radius;
}
}
// Usage
Shape myCircle = new Circle(5.0);
Console.WriteLine($"Area: {myCircle.GetArea()}");
myCircle.Display();
Understanding these core OOP principles is fundamental to building robust, scalable, and maintainable applications in .NET Core.