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 ("" - , , ) - , . ;
  • protected ("" - , , ) - ;
  • - , / .

C# (" "), ( ), :

  • internal ("" - ) - , ;
  • internal protected (" " - ) - , .

. .

encapsulation programming example












, , . , .

- . " ", .

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 deliberately will not be able to 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;
  • , - , ;
  • - - . - - . - .

encapsulation programming is




() :

* :

description - //, , , , . /

using System;

namespace OOPLibrary.Auto

{

///

/// ,

///

public class Auto

{

///

/// , , , ,

/// private, , (. ).

///

private int _age;

///

/// ( - , ), ,

/// , . ""

///

private bool _isMoving;

///

/// .

/// "".

///

public string Color;

///

/// ,

/// ( , ).

///

public string Name;

///

/// , ,

///

public Auto()

{

_age = 5;

_isMoving = false;

Color = "";

Name = " ";

}

///

/// . ?

/// .

///

/// .

public string GetAge()

{

return " " + _age + " .";

}

///

/// , . , , ( ), , , / .

///

public void Start()

{

if (_isMoving)

{

Console.WriteLine(" ");

}

else

{

_isMoving = true;

Console.WriteLine(" , .. ! !");

}

}

///

/// , . , .

///

public void Stop()

{

if (_isMoving)

{

_isMoving = false;

Console.WriteLine(", ");

}

else

{

Console.WriteLine(". , ");

}

}

///

/// ,

///

public void MoveLeft()

{

if (_isMoving)

{

Console.WriteLine(" ");

}

else

{

Console.WriteLine(". . ");

}

}

///

///

///

public void MoveRight()

{

if (_isMoving)

{

Console.WriteLine(" ");

}

else

{

Console.WriteLine(". . , .");

}

}

}

}




All Articles