Why do we Use Encapsulation?
Encapsulation is used to properly maintain programs that we write as well as programs written by others. It is a practice to keep code secure and operational rather than allowing it to degrade into a broken mess over time.
Encapsulation is commonly used in the following languages:

Let's examine two popular programming languages, Java
and Python
, and what encapsulation means in their context.
1. Encapsulation in Java
The following code creates a class
called College, with a private variable and method.
1public class College {
2//Some code here
3}
Only the class
creator can change its source code or call the hidden private methods. The method can only be used by the creator of the class
and would not be accessible to other users. With this in mind, we can see that encapsulation is used for applications that are expected to run for an extended period of time.
2. Encapsulation in Python
The following code creates a class
that has the ability to store some private data.
1class College:
2def output(self, destination=None):
3def __init__(self, address):
4def print_address(self, destination=None):
These can be completely hidden from other Python
code users. The data will only be used by the class
's creator and will not be accessible to other users. The output method of the class
will print any given value to a printer or display device