Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects. These objects are instances of classes and can contain data (fields) and methods (functions) that operate on the data. The core principles of OOP are:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Let's explore these concepts with examples in C# (which is commonly used in .NET).
1. Encapsulation
Encapsulation is the concept of wrapping data (variables) and methods that operate on the data into a single unit, called a class. It also restricts direct access to some of the object’s components, which is why the object's state is protected from unauthorized access.
Example:
csharp
public class Car
{
// Private field
private string _model;
// Public method to access the private field
public string Model
{
get { return _model; }
set { _model = value; }
}
// Method to display car details
public void DisplayCarDetails()
{
Console.WriteLine("Car Model: " + _model);
}
}
public class Program
{
public static void Main(string[] args)
{
// Create an object of Car
Car car = new Car();
car.Model = "Tesla"; // Setting the model
car.DisplayCarDetails(); // Displaying the car model
}
}
Here, the field _model
is private, and the property Model
allows controlled access to it.
2. Abstraction
Abstraction is the concept of hiding complex implementation details and showing only essential features to the user. This is typically achieved through abstract classes and interfaces.
Example (using Abstract Class):
csharp
public abstract class Animal
{
public abstract void Sound(); // Abstract method
public void Sleep()
{
Console.WriteLine("This animal is sleeping.");
}
}
public class Dog : Animal
{
public override void Sound()
{
Console.WriteLine("Woof! Woof!");
}
}
public class Program
{
public static void Main(string[] args)
{
Dog dog = new Dog();
dog.Sound(); // Outputs: Woof! Woof!
dog.Sleep(); // Outputs: This animal is sleeping.
}
}
Here, Animal
is an abstract class with an abstract method Sound()
. The Dog
class implements the abstract method.
3. Inheritance
Inheritance allows one class to inherit the properties and methods of another class. This promotes code reuse and establishes a relationship between parent and child classes.
Example:
csharp
public class Animal
{
public void Eat()
{
Console.WriteLine("Animal is eating");
}
}
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Dog is barking");
}
}
public class Program
{
public static void Main(string[] args)
{
Dog dog = new Dog();
dog.Eat(); // Inherited method from Animal
dog.Bark(); // Method of Dog
}
}
Here, Dog
inherits the Eat
method from Animal
.
4. Polymorphism
Polymorphism allows one interface to be used for a general class of actions. The specific action is determined by the object that is being referenced. This is achieved through method overriding (runtime polymorphism) or method overloading (compile-time polymorphism).
Example (using Method Overriding for Runtime Polymorphism):
csharp
public class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Animal makes sound");
}
}
public class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Dog barks");
}
}
public class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("Cat meows");
}
}
public class Program
{
public static void Main(string[] args)
{
Animal animal;
// Using runtime polymorphism
animal = new Dog();
animal.MakeSound(); // Outputs: Dog barks
animal = new Cat();
animal.MakeSound(); // Outputs: Cat meows
}
}
Here, the MakeSound
method is overridden in both Dog
and Cat
, and polymorphism is demonstrated by assigning different objects (Dog, Cat) to the same reference animal
.
Summary:
- Encapsulation: Wrapping data and methods together and restricting access.
- Abstraction: Hiding implementation details and showing only the necessary parts.
- Inheritance: A class inherits properties and behaviors from another class.
- Polymorphism: The ability of different objects to respond to the same method in different ways.
These four concepts make Object-Oriented Programming in .NET powerful for creating flexible, reusable, and maintainable code.
No comments:
Post a Comment