ComputersProgramming

Encapsulation is what? 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 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.

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, giving the 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 therefore the integrity of the data is preserved.

Terminology

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

The essence of the concept of "encapsulation"

The definition defines that encapsulation means concealing all or part of the 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 opened to the client class, and which ones are hidden.

Access Modifiers

There are such access modifiers that, among others, are able to 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 derivatives from it;
  • 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 encapsulation mechanism allows 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.

Object and Encapsulation

The combination of the implementation of the program 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 it is this technique that allows maintaining and ensuring 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 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 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.

Encapsulation Units

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

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) example:

* Note:

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

Using System;

Namespace OOPLibrary.Auto

{

///

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

///

Public class Auto

{

///

/// A variable created to write to it, how old is the car, since the external intervention in 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 of the age value 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 method implements 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. ");

}

}

}

}

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.birmiss.com. Theme powered by WordPress.