Understanding Object-Oriented Programming (OOP) in .NET
Object-Oriented Programming (OOP) is a programming style centered around objects and classes. It helps organize and manage code more effectively. In .NET, particularly in languages like C#, OOP principles are essential. Here’s a simple guide to key OOP concepts:
1. Classes and Objects
Class: Think of a class as a blueprint. It defines the properties and behaviors that objects created from it will have.
Object: An object is an instance of a class. It represents a specific example of a class, with its own unique values.
public class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public void StartEngine()
{
Console.WriteLine("Engine started.");
}
}
Car myCar = new Car();
myCar.Make = "Toyota";
myCar.Model = "Corolla";
myCar.Year = 2020;
myCar.StartEngine();
2. Encapsulation
Encapsulation is about bundling data and methods that operate on the data within one class. It also involves hiding internal details and exposing only what is necessary.
public class Person
{
private string name;
public string Name
{
get { return name; }
set
{
if (!string.IsNullOrEmpty(value))
name = value;
}
}
}
3. Inheritance
Inheritance allows one class to inherit the properties and methods of another class. This helps in reusing code and establishing a natural hierarchy between classes.
public class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Barking...");
}
}
Dog myDog = new Dog();
myDog.Eat(); // Inherited method
myDog.Bark(); // Own method
4. Polymorphism
Polymorphism allows for different classes to be treated through a common interface or base class. This can be achieved through method overriding and method overloading.
Method Overriding: Redefining a method in a subclass that already exists in a superclass.
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Some generic sound");
}
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Bark");
}
}
Animal myAnimal = new Dog();
myAnimal.MakeSound(); // Outputs: Bark
Method Overloading: Creating multiple methods with the same name but different parameters in the same class.
public class MathOperations
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
}
5. Abstraction
Abstraction involves hiding the complex implementation details and showing only the essential features of an object. This can be done using abstract classes and interfaces.
Abstract Class: A class that cannot be instantiated directly and can contain abstract methods which must be implemented by derived classes.
public abstract class Shape
{
public abstract double Area(); // Abstract method
public void Display()
{
Console.WriteLine("Displaying shape");
}
}
public class Circle : Shape
{
public double Radius { get; set; }
public override double Area()
{
return Math.PI * Radius * Radius;
}
}