Object-Oriented Programming (OOP) is a programming paradigm that focuses on designing software using objects, which are instances of classes. OOP aims to model real-world entities, their relationships, and their behaviors in a structured and modular way. C# is a powerful object-oriented programming language that fully supports OOP principles. Let's dive into the core concepts of OOP with multiple examples in C#:
Class and Object
A class is a blueprint or template for creating objects. An object is an instance of a class that encapsulates data (attributes) and behaviors (methods). For instance, consider a "Car" class:
class Car
{
public string Make;
public string Model;
public void StartEngine()
{
Console.WriteLine("Engine started");
}
}
// Creating objects
Car myCar = new Car();
myCar.Make = "Toyota";
myCar.Model = "Camry";
myCar.StartEngine();
Encapsulation
Encapsulation is the concept of bundling data (attributes) and methods (functions) that operate on that data within a single unit (class). It restricts direct access to internal data from outside the class and enforces controlled access through methods. This helps in maintaining data integrity and reducing unintended modifications.
class BankAccount
{
private double balance;
public void Deposit(double amount)
{
if (amount > 0)
balance += amount;
}
public double GetBalance()
{
return balance;
}
}
BankAccount account = new BankAccount();
account.Deposit(1000);
double currentBalance = account.GetBalance();
Inheritance
Inheritance allows one class (subclass/derived class) to inherit attributes and behaviors from another class (base class). It promotes code reuse and establishes a hierarchy among classes.
class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Woof!");
}
}
Dog myDog = new Dog();
myDog.Eat();
myDog.Bark();
Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common base class. It enables dynamic method binding and method overriding, allowing for flexibility in implementing specific behaviors for derived classes.
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a generic shape");
}
}
class Circle : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a circle");
}
}
class Square : Shape
{
public override void Draw()
{
Console.WriteLine("Drawing a square");
}
}
Shape[] shapes = new Shape[] { new Circle(), new Square() };
foreach (Shape shape in shapes)
{
shape.Draw();
}
Remember: A child class can do everything a base class can, so a child class object can always be assigned to a base type (such as a vehicle type holding a car object). This is called an is-a relationship. A child class is a base class. A car is a vehicle.
Base base = new Child(); //correct
However, a base class may not do everything a child class can, the child class may have its own set of properties and methods which the base class is not aware of. Therefore, assigning a base class object to a child class type is not valid. There is no is-a relationship here. A base class is NOT a child class. A vehicle is not a car (always)!
Child child = new Base(); //wrong!!
Abstraction
Abstraction involves defining the essential features of an object while hiding unnecessary details. It focuses on modeling the most relevant aspects of an object's behavior.
abstract class Animal
{
public abstract void MakeSound();
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Woof!");
}
}
class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("Meow!");
}
}
Encapsulation vs Abstraction
Encapsulation is about bundling data and methods together while controlling their access, while abstraction is about creating a simplified and focused representation of an object's behavior. They work together to provide clean and organized designs, making code easier to understand, maintain, and extend.
Here's a practical analogy:
Imagine you're driving a car (the object). Encapsulation is like having a dashboard (interface) that provides access to essential controls (methods) like steering, braking, and accelerating, while hiding the complexities of the engine and transmission.
Abstraction is like knowing how to drive a car without needing to understand the intricate mechanics of how the engine, transmission, and other components work together.
Conclusion
These are the core OOP concepts: class and object, encapsulation, inheritance, polymorphism, and abstraction. Utilizing these principles, you can create well-structured and modular code that models real-world entities and their relationships effectively.