Definition

encapsulation (object-orientated programming)

What is encapsulation (object-orientated programming)?

In object-oriented programming (OOP), encapsulation is the practice of bundling related data into a structured unit, along with the methods used to work with that data. Most OOP languages implement encapsulation primarily through classes and the objects instantiated through those classes. A class defines a set of attributes for the data and the methods used to carry out operations related to the data.

Through encapsulation, a class hides the implementation details of the programmed elements, while restricting direct access to those elements. It also provides a public interface for interacting with the class through its instantiated objects. In this sense, a class acts as a template for creating objects that can potentially access the same set of related methods and attributes.

object-orientated programming
Encapsulation in object-oriented programming enables a class to hide the implementation details of programmed elements, while restricting direct access to those elements.

A class is often defined for a specific category of related data, such as airplanes, pets, diseases, people, plants or any other type of information the program requires. Each object created from that class represents a specific item within the defined category. For example, a software developer building an application related to plants might create the Tree class, which might include three public members: the CommonName attribute, ScientificName attribute, and GetTreeInfo method.

Once the class has been defined, objects can be created based on the class, with each object representing a type of tree, such as ginkgo, mulberry, hawthorn or weeping willow. Because the three members in the Tree class permit public access, any object can access the CommonName and ScientificName attributes, as well as the GetTreeInfo method. However, if the class also contained members with limited access, those other classes would not be able to access these members nor would they have any knowledge of them.

Encapsulation and access modifiers

One of the defining characteristics of encapsulation is that it limits access to a class's attribute data and implementation details. To enforce these limits, OOP languages, such as Java, C++ and C#, use access modifiers that specify the type of access permitted at the class and member levels.

The exact modifiers used by an OOP language -- and the rules that govern them -- vary from one language to the next, but most of them support at least the following three modifiers:

  1. Public. The class or member can be accessed within the same class or within any other class in the software program.
  2. Private. The class or member can be accessed only within the class itself. It cannot be accessed by any other classes. Programming languages generally impose restrictions on how the private access modifier can be applied at the class level.
  3. Protected. The class or member can be accessed only within the class itself or within its derived classes. It cannot be accessed by any other classes. Programming languages generally impose restrictions on how the protected access modifier can be applied at the class level.

Using modifiers such as these, developers can control the extent to which classes, attributes and methods can be accessed by other classes within a program. For example, the following C# code defines the Tree class, which uses both the public and private access modifiers:

using System;

namespace Plants
{
  class Tree
  {
    public string CommonName;
    public string ScientificName;
    private string Family;
    public void GetTreeInfo()
    {
      Console.WriteLine("Common name: " + CommonName);
      Console.WriteLine("Scientific name: " + ScientificName);
    }
  }
  class Program
  {
    static void Main(string[] args)
    {
      Tree t = new Tree();
      t.CommonName = "Quaking Aspen";
      t.ScientificName = "Populus tremuloides";
      t.GetTreeInfo();
    }
  }
}

The Tree class includes four members. Three of the members -- the CommonName and ScientificName attributes and the GetTreeInfo method -- are defined with the public access modifier. Only the Family attribute is defined with the private access modifier. The GetTreeInfo method writes the values of the CommonName and ScientificName attributes -- along with other text -- to the console.

The code also defines the Program class, which creates an object named t based on the Tree class. It uses the t object to assign values to the CommonName and ScientificName attributes and then to run the GetTreeInfo method, which returns the values newly assigned to that object.

Because all three of these members are defined with the public access modifier, the Program class -- and other classes -- can access them as needed. However, if the Program class tries to access the Family attribute, C# will return an error stating, "Tree.Family is inaccessible due to its protection level." If all the members were defined with the private access modifier, the Program class could not access any of them, although it could still create an object based on the Tree class.

Encapsulation in networking

Encapsulation is also used extensively in networking, where it refers to the inclusion of one data structure within another structure so that the first data structure is hidden until it has been specifically decapsulated. Encapsulation plays a key role in most networking models, including TCP/IP and Open Systems Interconnection (OSI).

For example, when sending data from a source to a destination, TCP/IP encapsulates the data as it moves from the application layer to the transport layer. The application layer is at the top of the stack in the TCP/IP model, with the transport layer just below it. Each layer after the transport layer adds its own information to the packet as it travels to its destination.

OSI vs. TCP/IP networking models
Encapsulation in networking refers to the inclusion of one data structure within another so the first is hidden until it has been decapsulated. It is key to both the TCP/IP and OSI networking models.

The three top layers in the OSI model correspond to the application layer in TCP/IP. The top layer is application, the next is presentation and then there is the session layer. This is followed by the transport layer, which corresponds to the transport layer in TCP/IP. In the OSI model, data is encapsulated as it moves from the session layer to the transport layer, with the subsequent layers inserting additional information, just like with TCP/IP.

Learn more about object-oriented programming concepts.

This was last updated in April 2023

Continue Reading About encapsulation (object-orientated programming)

Dig Deeper on Network infrastructure