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.
Object-oriented programming stands on three pillars of its universe:
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.
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.
In C # ("C Sharp"), in addition to the specified (excluding the latter), there are still such modifiers:
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.
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.
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.
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.
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) 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>