Encapsulation is Hide Complexity
Encapsulation is one of the fundamental principle of Object-Oriented Programming (OOP) that promotes data Data security and Data hiding.
Encapsulation Binds the Properties and method into single unit.
You can implement encapsulation using Class and Access Modifiers (Public,Private,protected etc.)
Note : Encapsulated data can not access directly from out side of the class ,if requirement demands we can provide access through the public,internal or through protected methods, so we can do validation based on requirement rather than provide direct access.
Direct access issues :
Encapsulation intention is data security , so if you provide direct access we may face different problems like.
unauthorised persons may get access.
data may be misused
data can be deleted or modified.
What is the Benfits of giving encapsulated data access through methods.
Technically encapsulated data is private members so outside class can not accessible, so through public methods we can provide access. while you giving access through methods you do validations as per your requirement.
Encapsulation solves the security problems in Implementation level.
Features :
Modularity: Encapsulation promotes modularity by hiding the internal workings of a class from the outside world. This allows for easier maintenance and modification of code since changes to the internal implementation of a class won't affect other parts of the program that use it, as long as the interface remains unchanged.
Information Hiding: Encapsulation allows you to hide the details of how a class works, exposing only what is necessary for other parts of the program to interact with it. This prevents the direct access to the internal state of an object from outside the class, reducing the risk of unintended modifications and ensuring data integrity.
Abstraction: Encapsulation provides a way to abstract away complex implementation details, allowing developers to focus on using objects without needing to understand how they are implemented internally. This simplifies the programming model and makes code easier to understand and maintain.
Encapsulation of Complexity: By encapsulating data and behavior into a single unit, encapsulation helps manage complexity by breaking down a system into smaller, more manageable components. Each class encapsulates a specific set of responsibilities, making it easier to understand and reason about the code.
Controlled Access: Encapsulation allows you to control access to the internal state of an object by using access modifiers such as public, private, and protected. This ensures that data is accessed and modified only through well-defined interfaces, reducing the potential for bugs and making the code more robust.
Encapsulation Summary :
In summary, encapsulation is a fundamental principle of OOP that promotes data hiding, molecularity, information hiding, and improved security and flexibility in software design.
Encapsulation explanation through Cold capsule
In Real world we can say cold Capsule as Perfect example.
Explanation :- cold capsule is look like above picture , it is capsule it contains small blue balls and red balls inside 💊 capsule, these are sensitive materials which cure our cold problem,so these blue and red balls are encapsulated inside container, the container protects these sensitive content so these sensitive content people can not touch directly. only when people swallowed capsule then container get melt and sensitive content absorbs by human body.
so here blue balls representing as variables , red balls representing methods and container represented as class, so these variables and methods are encapsulated inside class so unauthorised persons can not access it directly from out side class but we can give access indirectly using methods on conditionally.
Watch 4 Min Video for Quick revision :
Watch Encapsulation In detailed video : Coming Soon
No comments:
Post a Comment
Thanks for the contribution, our team will check and reply back if response required.