What is encapsulation? Encapsulation in programming

Encapsulation is one of the three main features of object-oriented programming (OOP). The other two are polymorphism and inheritance. Together, they make up the OOP database, which defines a whole range of possibilities for writing programs in different languages, using these three principles. Object-oriented languages, in turn, are required to clearly follow them.

encapsulation is

OOP Basics

Object-oriented programming stands on three pillars of its universe:

  • To polymorphism, which answers the question of how a particular programming language interprets objects that are related to each other, in a similar way.
  • An inheritance that provides an answer on how code is stimulated repeatedly.
  • Encapsulation, which is the answer to the question of how the concealment of the implementation occurs, and therefore the preservation of data integrity.

Terminology

Encapsulation (programming) is the use of access modifiers to hide parts of the program code from the end user. By it, in turn, is meant a developer or an inheriting object.

encapsulation concepts

The essence of the concept of "encapsulation"

Definition determines that encapsulation means hiding all or part of the program code. The essence of the concept of "encapsulation" is to manipulate access modifiers. This means that the developer himself decides which properties, methods and classes will be open to the client class and which are hidden.

Access modifiers

encapsulation programming
There are access modifiers that, among others, can be manipulated by encapsulation (Java programming):

  • public ("public" - public, open, access) - general access for both current objects and classes, and for the outside world;
  • private ("private" - private, private, hidden access) - closed access, the essence of which is completely opposite to the previous one. Provides access only from the current class;
  • protected ("protected" - protected, half-hidden, access) - access for the current class and its derivatives;
  • by default - an unspecified access modifier implies that the field / method is visible for the entire current class package.

In C # ("C Sharp"), in addition to the above (excluding the latter), there are also such modifiers:

  • internal ("internal" - internal access) - general accessibility in the current collection, closed access for all other cases;
  • internal protected ("internal protected" - internal secure access) - the combination of two modifiers into one, in which the properties of both of them are manifested.

The role of encapsulation

The encapsulation mechanism eliminates the external influence on the program code and the incorrect use of the data contained in it. This is due to the combination of code and data into a single whole.

encapsulation programming example

Object and Encapsulation

The combination of the implementation of a software module and the data embedded in the code in programming is called an object. The essence of its connection with encapsulation lies in the fact that just such a technique allows you to maintain and ensure the holistic functioning of the mechanism in question.

The advantage of encapsulation

Encapsulation is a way to simplify the encoding process. Numerous lines of code remain behind the scenes, while in the main class work with instances of objects.

Data Protection Idea

Encapsulation is also a mechanism that implements the idea of ​​data protection. The program logic of object-oriented programming is based on the fact that most of the data will be hidden by the access modifier private (private, private) or protected (protected). The outside world, the client will not accidentally or intentionally damage the implementation of the software module. Since it is actually very easy to do this, not even on purpose, encapsulation is a very good principle.

Encapsulation Units

The class, as the main unit of encapsulation, describes the data and contains a code that can operate on this data. It is also the base for building an object. The latter, in turn, is presented as an instance of the class.

encapsulation java programming
The following terminology is also used:

  • members are the code and data included in the class;
  • fields, or instance variables - this is the name of the data that the class defines;
  • member functions - they contain the code itself. Member functions is a common name. A special case is the methods.

Case Study Encapsulation

encapsulation programming is

Encapsulation (programming) example:

* Note:

description is a description of the method / property / variable, that is, commenting on what actually happens in the program. Demonstrated with opening / closing tags

using System;

namespace OOPLibrary.Auto

{

///

/// This class is intended to describe the properties and actions of the car

///

public class Auto

{

///

/// A variable created in order to write to it how old the car is, since the developer considers it unnecessary to interfere with this property

/// it is marked with the private modifier, that is, private, private access (see description above).

///

private int _age;

///

/// Boolean variable (only two possible values ​​- either yes or no), which describes whether the car is currently moving

/// It also should not be open to the end user, no matter who he is. Therefore, this variable is assigned a private access modifier "private"

///

private bool _isMoving;

///

/// This string variable should contain information about the color of the car. It may be subject to changes due to external influences.

/// therefore, the public modifier public is selected for Color.

///

public string Color;

///

/// In this particular case, we assume that the name of the car can also be changed

/// the public modifier is assigned (open access for everyone, regardless of class or assembly).

///

public string Name;

///

/// The class constructor opens and all the properties expressed by variables and set a little earlier get their values

///

public Auto ()

{

_age = 5;

_isMoving = false;

Color = "Violet";

Name = "Skoda Octavia";

}

///

/// The method implements the return value of the age of the car. Why is this necessary?

/// private access modifier does not make it possible for the client to change it.

///

/// Returns the age of the car.

public string GetAge ()

{

return "Currently selected machine" + _age + "years.";

}

///

/// If the car does not move, this method implements the start of movement. A variable is checked, which indicates the state of the car (whether it is traveling or not), and, depending on the results, the corresponding action is performed / the corresponding message is displayed.

///

public void Start ()

{

if (_isMoving)

{

Console.WriteLine ("Movement has already been started");

}

else

{

_isMoving = true;

Console.WriteLine ("Go to the start, attention .. Go ahead! Let's go!");

}

}

///

/// If the movement was started, then this method stops it. The same program logic as in the previous case.

///

public void Stop ()

{

if (_isMoving)

{

_isMoving = false;

Console.WriteLine ("Stop, machine");

}

else

{

Console.WriteLine ("Error. The car and so it stands still, does not move");

}

}

///

/// Turn left if the vehicle is moving

///

public void MoveLeft ()

{

if (_isMoving)

{

Console.WriteLine ("Turn left");

}

else

{

Console.WriteLine ("Error. The car stands still. The turn function is currently not available");

}

}

///

/// A similar method with turning to the right

///

public void MoveRight ()

{

if (_isMoving)

{

Console.WriteLine ("Turn right was successful");

}

else

{

Console.WriteLine ("Error. The car has not yet moved. Turning to the right is currently an action impossible to complete.");

}

}

}

}

Source: https://habr.com/ru/post/K853/


All Articles