Why encapsulation is required




















How do we prevent accessing the fields directly? What is the real use of encapsulation? The user can enter a value of , which although is a valid number, is an invalid age. A setter method could have logic which would allow you to catch such things. Another scenario, would be to have the age field, but hide it. You could also have a Date of Birth field, and in it's setter you would have something like so:. I have also been confused like you too for a long time until I read the book Encapsulation and Inheritance in Object-Oriented Programming Language and a website that explained the importance of Encapsulation.

I was actually directed from the website to the book. People always say encapsulation is "hiding of information" therefore, maybe, making encapsulation focus on security as the main use. Yes you are hiding information in practice, but that should not be the definition as it could confuse people. Encapsulation is simply "minimizing inter-dependencies among separately-written modules by defining strict external interfaces" quoting from the book.

That is to say that when I am building a module, I want a strict contract between my clients and me on how they can access my module. Because their "module" does not exactly depend on the Inner workings of my module but depends on the "external interface", I made available to them. So, if I don't provide my client with a setter and give them direct access to a variable, and I realize that I need to set some restriction on the variable before my client could use it, me changing it, could be me, changing the life of my client, or application of my client with HUGE EXPENSE.

But if I provided the "strict contract" by creating a "strict external interface" i. In the setter situation using encapsulation , if it happens that when you set a variable, and I return a message informing you that it has been assigned, now I could send a message via my "interface", informing my client of the new way my module have to be interacted with, i. But if I did not use encapsulation, and gave my client direct access to a variable and I do my changes, it could result in a crashed system.

Because if the restriction I implemented, is that, you could not save negatives and my client have always been able to store negatives, my clients will have a crashed system in their hands if that "crashed system" was a banking system, imagine what could happen. So, encapsulation is more about reducing dependency between module, and an improvement can be made "quietly" with little or no expense to other modules interacting with it, than it is of security.

Because the interacting modules depend on the "strict external interface or strict contract". You're not exactly preventing access to the fields -- you're controlling how others can access certain fields.

For example you can add validation to your setter method, or you can also update some other dependent field when the setter method of a field is called. You can prevent write or read access to the field e. A setter may check if the value is valid.

It may ask a SecurityManager if you should be allowed to do this. It may convert between data types. And so on. For example you have: private double salary, you setter method could restrict that only hr staff can change the salary field it could be written as:. Imagine if salary was public and could be access directly any can change it however and whenever they want, this basically the significance of encapsulation.

Encapsulation doesn't expose how your class is working internally. It gives you more freedom to change how your class works. It gives you the option to control the access to your class. You can check if what the user enters is valid you don't want the user to enter a day with a value of The main idea behind encapsulation is data hiding.

There are several reasons why we use encapsulation in object oriented programming. Some of the identified reasons for why we encapsulation are as follows The real use of encapsulation. Better maintainability : When all the properties are private and encapsulated, it is easy for us to maintain the program simply by changing the methods. Make Debugging Easy : This is in line with the above point. A change like this would cost millions. We would need to change all gas pumps, not to mention mechanical shops and auto parts.

When we break encapsulation we have to pay a price. This last part of our analogy, clearly reveals that failing to define proper abstractions with proper levels of encapsulation will end up causing difficulties when change finally happens. So, as we can see, the goal of encapsulation is reduce the complexity of the abstractions by providing a way to hide implementation details and it also help us to minimize interdependence and facilitate change.

We maximize encapsulation by minimizing the exposure of implementation details. However encapsulation will not help us if we do not define proper abstractions. Simply put, there is no way to change the public interface of an abstraction without breaking its users.

So, the design of good abstractions is of paramount importance to facilitate the evolution of the APIs, encapsulation is just one of the tools that help us create this good abstractions, but no level of encapsulation is going to make a bad abstraction work. One of those things that we always want to encapsulate is the state of a class. The state of a class should only be accessed through its public interface.

In a object-oriented programming language like Java, we achieve encapsulation by hiding details using the accessibility modifiers i. With these levels of accessibility we control the level of encapsulation, the less restrictive the level, the more expensive change is when it happens and the more coupled the class is with other dependent classes i.

In object-oriented languages a class has two public interfaces: the public interface shared with all users of the class, and the protected interface shared with subclasses. It is of paramount importance that we design the proper levels of encapsulation for every one of these public interfaces so that we can facilitate change and foster evolution of our APIs. Many people wonder why we need accessor and mutator methods in Java a. But the purpose of encapsulation here is is not to hide the data itself, but the implementation details on how this data is manipulated.

So, once more what we want is a way to provide a public interface through which we can gain access to this data. We can later change the internal representation of the data without compromising the public interface of the class. We consider it bad practice to retrieve the information from inside the object and write separate code to perform the action outside of the object.

Some languages have features which allow us to enforce encapsulation strictly. In Java it is also considered good practice to write setters and getters for all attributes, even if the getter simply retrieves the attribute and the setter just assigns it the value of the parameter which you pass in. Skip to content Overview Encapsulation is one of the fundamentals of OOP object-oriented programming. Virtualization allowed multiple operating systems and applications to run at the same time while sharing the resources of a single computer.

Containers improved on this model by sharing a host operating system and installing the container runtime engine onto the host machine's operating system. A computer running four virtual machines expends additional resources for each instance of the operating system that it runs, while a computer with an installed containerization engine could run the same number of applications on a single operating system.

As a result, multiple containers can operate using the same computing capacity as a single virtual machine. Containers are just one example of encapsulation in coding where data and methods are bundled together into a single package. As we mentioned earlier, encapsulation in object oriented programming allows developers to bundle data and methods together but it can also be used to hide sensitive data that should not be exposed to users.

A getter method is used to retrieve the value of a specific variable within a class. A setter method is used to set or update the value of a specific variable within a class. Programmers can use access modifiers to define the visibility and accessibility of classes, along with the data and methods that they contain.

In the Java programming language, there are four types of access modifiers to choose from:. There are many benefits to hiding information about attributes and methods using encapsulation in programming. One is that it prevents other developers from writing scripts or APIs that use your code. With encapsulation, users of a class do not learn how a class stores its data and the developer can change the data type of a field without forcing developers and users of the class to change their code.

The encapsulation process helps to compartmentalize data, limiting vulnerabilities by providing users with information on code implementations exclusively on a need-to-know basis.



0コメント

  • 1000 / 1000