Professional Documents
Culture Documents
Introduction:
At the heart of OOP in C# are classes and objects. A class is a blueprint for creating objects, and an
object is an instance of a class. Classes encapsulate data and behavior, providing a way to structure and
organize code. Let's look at a simple example:
```csharp
class Car
{
public string Model;
public int Year;
2. **Encapsulation:**
Encapsulation is the concept of bundling data and methods that operate on that data within a single unit,
i.e., a class. Access modifiers such as `public`, `private`, and `protected` control the visibility of members.
This ensures that the internal details of a class are hidden from the outside world, promoting a more
secure and modular codebase.
```csharp
class BankAccount
{
private decimal balance;
3. **Inheritance:**
Inheritance allows a class to inherit properties and methods from another class, fostering code reuse.
The base class (parent) provides a foundation, and the derived class (child) extends or modifies its
behavior.
```csharp
class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
4. **Polymorphism:**
Polymorphism allows objects of different types to be treated as objects of a common type. In C#, this is
achieved through method overloading and interface implementation.
```csharp
interface Shape
{
void Draw();
}
// Polymorphic behavior
Shape myShape = new Circle();
myShape.Draw();
Abstraction involves simplifying complex systems by modeling classes based on essential properties
and behaviors. Abstract classes and interfaces in C# help achieve abstraction.
```csharp
abstract class Shape
{
public abstract void Draw();
}
Conclusion: