Encapsulation in OOP: What Is It and How Does It Work?

Written by Coursera Staff • Updated on

Learn all you need to know about encapsulation in oop, how it works, how to use it, and how it differs from other object-oriented concepts.

Learn all you need to know about encapsulation in OOP, how it works, how to use it, and how it differs from other object-oriented concepts.

Encapsulation is a fundamental concept of object-oriented programming (OOP) with many programming benefits. It is one of four OOP concepts with a unique purpose, the other three being inheritance, polymorphism, and abstraction. Continue reading to learn what encapsulation in OOP is, how it works, the different types of encapsulation, its benefits, and how it differs from other OOP concepts. 

Encapsulation defined

Let’s begin by defining encapsulation. Encapsulation is a way to bundle coding pieces together, allowing for greater security and simplifying data hiding. 

How encapsulation works

Generally speaking, encapsulation works by limiting access to certain data based on the specific information the user needs at that time. Only the information the user needs to complete their task is provided. How encapsulation works depends explicitly on the type of encapsulation being used. 

Types of encapsulation in OOP

Object-oriented programming has three ways to implement encapsulation: member variable, function, and class. 

Member variable encapsulation

In member variable encapsulation—also called data member encapsulation—data members are class members. Classes are typically private by default, so data members are also generally private members of a class. The setter and getter method is required to access data members within the encapsulation. 

Function encapsulation

Function encapsulation refers to indicating a method or block of code as private to prevent access by those not authorised to see it. 

Class encapsulation

Like function encapsulation, a class must be declared private to remove user access. Class encapsulation allows a particular class to be hidden or inaccessible to other classes. 

Benefits of using encapsulation

Encapsulation can have many benefits, but we will focus on the top three: information hiding, reusability, and improved maintenance and clarity. 

Hides information

By using access modifiers, you can hide objects from a user's view. This removes a user's ability to access and change the code in any way. 

Easy to reuse

Encapsulation reusability stems from its ability to quickly create many objects by defining only one class. 

Improves maintenance and clarity

Debugging and maintaining a code is simple, with all data compartmentalised through encapsulation. This is because all the data that needs maintenance is in one class and does not affect other classes. 

Hiding information using encapsulation

Encapsulation can be a powerful tool for protecting data from unauthorised users through information hiding. Object-oriented programming has four types of access modifiers: public, private, protected, and default. Which modifier you use will depend on the level of protection your code requires. However, a good rule of thumb is to select the modifier with the most significant protection that doesn’t hinder your code's functionality. 

  • Public: If a class is declared public, any other class can access it without restriction.

  • Protected: Under the protected modifier, a class can be accessed from within itself and from inherited classes. Uninherited classes can still not access the class if it is declared protected. 

  • Private: Classes declared private are only accessible through member methods within that particular class. 

  • Default/no modifier: If a class does not have an access modifier assigned to it, it is given the default access modifier. This means that any class within the same package can access the member. 

Encapsulation vs inheritance vs abstraction vs. polymorphism

OOP includes other functions related to encapsulation. While they have similarities, each concept is unique and serves a specific purpose. Let’s distinguish between encapsulation, inheritance, abstraction, and polymorphism. 

Encapsulation vs inheritance

Inheritance is the concept of new classes acquiring data from existing classes, while encapsulation, as we know, is the concept of compartmentalising related data into segments/groups.  

Inheritance creates hierarchies within the code so that a new class can inherit the information from the class(es) below it. 

Encapsulation vs abstraction

Abstraction and encapsulation may be the most similar object-oriented programming concepts we will discuss. The difference between the two may be small, but it is important. 

Both encapsulation and abstraction can hide information. The difference is that abstraction hides data to create clarity for the user. Encapsulation, on the other hand, compartmentalises data. The action of hiding data is simply a use for already encapsulated data. 

Encapsulation vs polymorphism

Polymorphism and encapsulation are similar in that they both work to combine information. Encapsulation does this by creating groupings of related data. Polymorphism uses a single form or interface that can be used differently for different applications. 

There are two types of polymorphism: compile-time and run-time. As their names suggest, compile-time polymorphism can take many forms during compilation, and run-time polymorphism can take many forms while running. 

Next steps

Encapsulation is just a small part of object-oriented programming. To expand your programming knowledge, consider exploring Introduction to Object-Oriented Programming With Java or Object-Oriented C++: Inheritance and Encapsulation on Coursera. 

Keep reading

Updated on
Written by:

Editorial Team

Coursera’s editorial team is comprised of highly experienced professional editors, writers, and fact...

This content has been made available for informational purposes only. Learners are advised to conduct additional research to ensure that courses and other credentials pursued meet their personal, professional, and financial goals.