Sumo Logic ahead of the pack
Read articleComplete visibility for DevSecOps
Reduce downtime and move from reactive to proactive monitoring.
Encapsulation is a way to restrict the direct access to some components of an object, so users cannot access state values for all of the variables of a particular object. Encapsulation can be used to hide both data members and data functions or methods associated with an instantiated class or object.
Encapsulation in programming has a few key benefits. These include:
Encapsulation in Java
Object-oriented programming is one of the key pillars of Java. The concepts here refer to object-oriented programming as a whole and Java development. To become a Java developer it is essential to understand the concepts of encapsulation, inheritance, abstraction, and polymorphism.
Along with inheritance, encapsulation and abstraction form the three central principles of object-oriented programming.
What is inheritance in programming?
Inheritance is a mechanism that allows one class to gain the properties of another class, in the same way, that a child inherits some attributes from each of their parents. Inheritance allows programmers to create a new class that reuses the data members and methods of an existing class.
What is abstraction in programming?
Abstraction occurs when a programmer hides any irrelevant data about an object or an instantiated class to reduce complexity and help users interact with a program more efficiently. The term abstraction vs encapsulation can be used to describe the process of hiding some of the information contained in an object or class, but it may also refer to the object itself. An abstraction is any named entity that contains a selection of data and behaviors specific to a particular usage of the originating entity.
In the popular programming text Object-Oriented Analysis and Design, Grady Booch writes that:
"Abstraction and encapsulation are complementary concepts: abstraction focuses on the observable behavior of an object...encapsulation focuses on the implementation that gives rise to this behavior"
Stated differently, an abstraction relates to how an object and its behaviors are presented to the user and encapsulation is a methodology that helps create that experience.
Think about the interface of your mobile phone. Whether you use an Android operating system or iOS, you don't interact directly with the code that allows your phone to connect to the internet, send a text message or play a video game. Instead, you interact with the code through a user interface that is designed to streamline your experience and make it easy to access the functions and methods you need to complete a task. In this case, the interface is abstracted away from the actual implementation of the code.
Containers are a relatively new type of software that can be used to virtually package a piece of code along with all of its libraries and other dependencies that it needs to execute. Containers create an encapsulated virtual environment where an application can be launched using the minimum amount of storage space and computing power. A group of containers can share access to a single operating system and draw their computing resources from a single piece of hardware.
Encapsulation in OOP: containers vs. virtual machines
Containerization has emerged as an alternative to virtual machines because of its highly efficient usage of computing resources. 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.
In the Java programming language, and in many other languages, information hiding is controlled using getter/setter methods for data attributes that will be readable or that may be updated by other classes.
Encapsulation in OOP: getter/setter methods
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.
IT organizations can implement encapsulation to protect sensitive data and maintain compliance with industry-specific data security and privacy requirements such as HIPAA and PCI DDS. The encapsulation process helps to compartmentalize data, limiting vulnerabilities by providing users with information on code implementations exclusively on a need-to-know basis.
Sumo Logic complements your cyber security measure with cutting-edge threat detection and security analytics powered by artificial intelligence. Get started with Sumo Logic for free.
Reduce downtime and move from reactive to proactive monitoring.