Encapsulation concept

I have a problem with the concept and implementation of encapsulation.

Can someone explain this to me?

0
encapsulation
source share
4 answers

Encapsulation is a moderately light concept, as soon as you realize it (probably) comes from the same basic word as the capsule.

This is just containment of information.

Encapsulation means that the class publishes only what is necessary for others to use it, and nothing more. This is called information hiding, and this means that classes can completely change their insides without affecting any of their users.

In other words, a dictionary class can start life as a simple array and go to a binary tree of words, and then, possibly, to some database access functions without changing the interface to it.

In an object-oriented world, objects store both their data and the methods used to manage the data, and this is the pinnacle of encapsulation. One way to do this is to make sure that each object knows which functions to call in order to manipulate its data, and to ensure that the correct ones are called.

As an example, here is a class for storing entire lists in my mythical but oddly pythonic and therefore hopefully easy to understand language:

class intlist: private int val[10] private bool used[10] public constructor: for i in 0..9: used[i] = false public function add (int v): for i in 0..9: if not used[i]: used[i] = true val[i] = v return throw intlist-full public function del (int v): for i in 0..9: if used[i] and val[i] == v: used[i] = false return throw intlist-invalid-value 

Now, the only information published here is the constructor and two functions for adding and removing.

Since everything else is encapsulated, I can change it as I see fit without breaking the code that uses it.

I could make arrays longer, I could store them sorted or in a binary tree instead of an array to make it faster. Until the published API changes, I am free to do what I want. In fact, I can also add things to the API without breaking any other code, I just cannot remove or change everything that they rely on.


It should be noted that encapsulation is not something new with object orientation. This has been a whole century in C, ensuring that information is hidden in the module (usually this is the source file or its group with private headers).

In fact, stdio.h FILE* stuff is a good example of this. You don't care what really stands behind the pointer, since all the functions that use it know how to do their stuff.

+10
source share

link text

I always explain this to people, thinking of myself as an object. Other people can see your height, they can see if you are smiling, but your inner thoughts may be the reason you smile, only you know.

+7
source share

Encapsulation - transferring data in one block. we can also say that we hide information about the necessary details. example You have a mobile phone ... there is some kind of interface that helps you interact with a mobile phone, and you can use the services of a mobile phone. But actually working in a cell phone is hiding. u don't know how it works inside.

hide / bind something : for example: a capsule (which we consume when vr ill) to hide / bind some kind of powder form by itself means that the capsule encapsulates the powder containing it.

Linking data and behavior Object functionality in a secure and controlled manner.

or the best example of encapsulation is CLASS, because the class hides class variables / functions from the outer class d.

Encapsulation: Combining data and method elements together into a single block (such as a class) is called encapsulation.

For example: we can consider a capsule. Encapsulation means hiding the internal details of an object, that is, how the object does something. Here, the capsule is a single unit containing many things. But we cannot see what is in the side capsule.

This is the method used to protect information about an object from other objects. Like a variable, we can set both private and public property. When we access a property, we check and set it.

We can move on to other examples. Our laptop. We can use a laptop, but what operations take place inside that we do not know. But we can use it. Same as mobile, TV, etc.

We can conclude that a group of related properties, methods, and other members is considered as a single object or object. An encapsulated object is often called an abstract data type.

There are several other ways to use encapsulation, as an example we can use the interface. The interface can be used to hide information about the implemented class.

// declare as private

  private string _LegName; 

// Set as public property

 public string LegName { get { return _LegName; } set { _LegName=value; } public class LegMain { public static int Main(string[] args) { Leg L= new Leg(); d.LegName="Right Leg"; Console.WriteLine("The Legis :{0}",d.LegName);return 0; } } 

Note. Encapsulation provides a way to protect data from accidental damage.

thanks

+2
source share

Encapsulation is not just defining access methods and mutators for a class. This is a broader concept of object-oriented programming, consisting in minimizing the interdependence between classes, and it is usually implemented by hiding information.

The beauty of encapsulation is the power of changing things without affecting its users.

In an object-oriented programming language such as Java, you achieve encapsulation by hiding details using accessibility modifiers (public, protected, private, plus no modifier, which means closing the package). With these accessibility levels, you control the level of encapsulation, the less restrictive the more expensive change happens when it happens, and the more the class is associated with other dependent classes (for example, custom classes, subclasses).

Therefore, the goal is not to hide the data, but in the implementation details of how to manipulate this data.

The idea is to provide an open interface through which you access this data. You can subsequently change the internal representation of the data without compromising the public interface of the class. On the contrary, by exposing the data itself, you break encapsulation and, therefore, the ability to change the way data is managed without affecting its users. You are creating a dependency with the data itself, not with the public interface of the class. You will create the perfect cocktail for trouble when the โ€œchangeโ€ finally finds you.

There are several reasons why you can encapsulate access to your fields. Joshua Bloch in his book "Effective Java" in paragraph 14: Minimize the availability of classes and members, mentions several good reasons that I quote here:

You can limit the values โ€‹โ€‹that can be stored in the field (i.e. the floor must be F or M). You can take action when the field changes (trigger event, check, etc.). You can ensure thread safety by synchronizing the method. You can switch to a new view of the data (for example, computed fields, a different data type) However, encapsulation is more than hiding fields. In Java, you can hide entire classes, thereby hiding implementation details of the entire API. Think, for example, in the Arrays.asList () method. It returns a List implementation, but you don't care about which implementation, if it satisfies the List interface, right ?. The implementation may be changed in the future without affecting the users of the method.

The beauty of encapsulation

Now, in my opinion, to really understand encapsulation, you first need to understand abstraction.

Think, for example, about the level of abstraction in a car concept. The car is complex in its internal implementation. They have several subsystems, such as transmission system, fault system, fuel system, etc.

However, we have simplified its abstraction, and we interact with all cars in the world through the public interface of their abstraction. We know that all cars have a steering wheel through which we control the direction, they have a pedal, when you press it, you accelerate the car and control the speed, and the other when you press it, you stop it, and you have There is a mechanism that allows you to control if you are going forward or backward. These functions represent the public interface of the car abstraction. In the morning, you can drive a sedan, and then get out of it and drive an SUV during the day, as if it were the same.

However, few of us know the details of how all these functions are implemented under the hood. Think about the time when cars did not have a hydraulic steering system. One day, car manufacturers invented it, and they decide to get on it. However, this did not change the way users interact with them. At best, users experienced an improvement in using a directional system. Such a change was possible because the internal implementation of the car is encapsulated. Changes can be safely made without affecting the public interface.

Now think that car manufacturers have decided to put a fuel cap under the car, and not on one of its sides. You go and buy one of these new cars, and when you run out of gas, you go to the gas station and you wonโ€™t find the fuel cap. Suddenly you realize that you are below the car, but you cannot reach it with the gas pump hose. Now we have violated the public interface agreement, and therefore the whole world is breaking, it is falling apart because things do not work as expected. Such a change would be worth millions. We will need to replace all gas pumps in the world. When we break encapsulation, we have to pay the price.

So, as you can see, the goal of encapsulation is to minimize interdependence and facilitate change. You maximize encapsulation by minimizing the impact of implementation details. The state of a class should be accessible only through its public interface.

I really recommend you read Alan Snyder's article, Encapsulation and Inheritance, in object-oriented programming languages. This link points to the original ACM paper, but I'm sure you can find the PDF copy through Google.

+1
source share

All Articles