Abstraction:
- Abstraction is the process of simplifying complex reality by modeling classes based on their essential properties and behaviors.
- It involves creating classes that represent real-world entities or concepts, while ignoring or hiding unnecessary details.
- Abstraction focuses on defining the high-level structure of classes, their relationships, and the essential methods and properties they should have.
- The purpose of abstraction is to provide a clear and concise view of objects in a system and to allow developers to work with a higher level of understanding.
- Interfaces, abstract classes, and base classes are often used to achieve abstraction by providing a common template that other classes can follow.
public abstract class Vehicle
{
public abstract void Start();
public abstract void Stop();
}
public class Car : Vehicle
{
public override void Start()
{
Console.WriteLine("Car started");
}
public override void Stop()
{
Console.WriteLine("Car stopped");
}
}
public class Motorcycle : Vehicle
{
public override void Start()
{
Console.WriteLine("Motorcycle started");
}
public override void Stop()
{
Console.WriteLine("Motorcycle stopped");
}
}
In this example, the Vehicle class represents an abstract concept of a vehicle. It defines the abstract methods Start and Stop, without providing the implementation. The Car and Motorcycle classes inherit from Vehicle and provide their own implementations for these methods. The abstraction here is that we've defined a common interface for different types of vehicles, without specifying how each type actually starts or stops.
Encapsulation
Encapsulation is the practice of bundling data (attributes) and methods (functions) that operate on that data into a single unit, known as a class. It also involves controlling the access to the internal state of an object.
- Encapsulation is the practice of bundling data (attributes) and the methods (functions) that operate on that data into a single unit, called a class.
- It hides the internal details of how an object works and exposes only the necessary methods and properties that should be visible to other parts of the program.
- Encapsulation promotes the idea of data hiding, where the internal state of an object is not directly accessible from outside the class. Instead, controlled access is provided through well-defined interfaces.
- The primary goal of encapsulation is to ensure data integrity, protect sensitive information, and prevent unauthorized modifications to an object's state.
- Access modifiers (such as private, protected, and public) are used to control the visibility and accessibility of members within a class.
Here's an example of encapsulation using a Person class:
public class Person
{
private string name;
private int age;
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
public void DisplayInfo()
{
Console.WriteLine($"Name: {Name}, Age: {Age}");
}
}
In this example, the Person class encapsulates the data (name and age) and provides methods (DisplayInfo) to operate on that data. The data members are kept private, and access to them is controlled through public properties (Name and Age). This encapsulation helps ensure that the internal state of the object is not directly accessible from outside and can only be modified through defined methods.
Difference between Abstraction and Encapsulation
- Abstraction focuses on exposing only relevant information and behavior, while hiding unnecessary details. It defines a higher-level view of an object.
- Encapsulation focuses on bundling data and methods that operate on that data into a single unit. It controls access to the internal state of an object, ensuring data integrity and security.
In summary, abstraction deals with the visibility of an object's behavior, while encapsulation deals with the visibility and access control of an object's internal state. Both concepts contribute to better-designed, modular, and maintainable code.
No comments:
Post a Comment