SITE SEARCH

Encapsulation is what? Encapsulation in programming

Encapsulation is one of the three mainfeatures of object-oriented programming (OOP). The other two are polymorphism and inheritance. Together they form the base of the PLO, which determines the whole range of possibilities for writing programs in different languages, using these three principles. Object-oriented languages, in turn, are required to follow them clearly.

encapsulation is

OOP Basics

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

  • Polymorphism, responding to the question of how a certain programming language treats objects that have a relationship with each other, in a similar way.
  • Inheritance, which gives an answer, how the stimulation of the use of the code occurs repeatedly.
  • Encapsulation, which is the answer to the question of how the implementation is concealed, and thus the integrity of the data is preserved.

Terminology

Encapsulation (programming) isuse of access modifiers to hide parts of the program code from the end user. Under it, in turn, means the developer or the inheriting object.

concepts of encapsulation

The essence of the concept of "encapsulation"

The definition defines that under encapsulationit implies the concealment of all or part of the program code. The essence of the concept of "encapsulation" is the manipulation of access modifiers. This means that the developer himself decides which properties, methods and classes will be open to the client class, and which ones are hidden.

Access Modifiers

encapsulation programming
There are such access modifiers that, among others, can manipulate encapsulation (Java programming):

  • public (public - public, open, access) - shared access both for current objects and classes, and for the outside world;
  • private ("privately" - private, private, hidden access) - private access, the essence of which is completely opposite to the previous one. Provides access only from the current class;
  • protected (protected, semi-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 specified (excluding the latter), there are still such modifiers:

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

Role of Encapsulation

The mechanism of encapsulation allows you to exclude external influence on the program code and incorrect use of the data embedded in it. This is done by combining the code and data into one.

encapsulation programming example

Object and Encapsulation

Integration of the implementation of the software module andData embedded in the code in programming is called an object. The essence of its connection with encapsulation lies in the fact that it is this technique that allows to maintain and ensure the integrity of the mechanism in question.

Advantage of encapsulation

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

The idea of ​​data protection

Encapsulation is also a mechanism thatrealizes 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 external world, the client accidentally or intentionally can not damage the implementation of the software module. Since in fact it is very easy to do this even on purpose, encapsulation is a very good principle.

Units of encapsulation

Class, as the basic unit of encapsulation,describes the data and contains the code that is capable of operating with these data. It is also the base for constructing an object. The latter, in turn, is represented as an instance of the class.

encapsulation programming java
The following terminology is also used:

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

Encapsulation on a concrete example

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 a car

///

public class Auto

{

///

/// A variable created to write to it, how old is the car, since the external intervention into this property is considered by the developer to be superfluous

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

///

private int _age;

///

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

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

///

private bool _isMoving;

///

/// This string variable must contain information about the color of the car. It can be subject to changes from external influences

/// because the public access modifier "public" was chosen for the color.

///

public string Color;

///

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

/// assigns a public modifier (public access for everyone, regardless of class or assembly).

///

public string Name;

///

/// The class constructor is opened, and all the properties expressed by variables and specified earlier, get their values

///

public Auto ()

{

_age = 5;

_isMoving = false;

Color = "Purple";

Name = "Skoda Octavia";

}

///

/// The method implements the return value of the age of auto. Why is it necessary?

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

///

/// Returns the age of the car.

public string GetAge ()

{

return "At the moment, the selected machine is" + _age + "years old.";

}

///

/// If the car does not move, this methodrealizes the start of the movement. A check is made to the variable indicating the state of the car (whether it is traveling or not), and, depending on the results, the corresponding action is performed / a corresponding message is displayed.

///

public void Start ()

{

if (_isMoving)

{

Console.WriteLine ("The movement has already been started");

}

else

{

_isMoving = true;

Console.WriteLine ("To start, attention .. Forward! Let's go!");

}

}

///

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

///

public void Stop ()

{

if (_isMoving)

{

_isMoving = false;

Console.WriteLine ("Stop, machine");

}

else

{

Console.WriteLine ("Error: the car is already in place, does not move");

}

}

///

/// Turn left if there is a vehicle movement

///

public void MoveLeft ()

{

if (_isMoving)

{

Console.WriteLine ("Turned left");

}

else

{

Console.WriteLine ("Error: The car is stationary, the rotation function is currently unavailable");

}

}

///

/// Similar method with rotation to the right

///

public void MoveRight ()

{

if (_isMoving)

{

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

}

else

{

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

}

}

}

}

</ p>
  • Rating: