What is the meaning of data hiding

One of the most important aspects of OOP is data hiding. Can someone explain, using a simple piece of code, what exactly hides the data and why do we need it?

+3
source share
7 answers

I guess, hiding the data, that you mean something like encapsulation or having a variable inside an object and only exposing it to get and modify methods, usually when you want to apply some logic to make a value set?

public class Customer
{
    private decimal _accountBalance;

    public decimal GetBalance()
    {
        return _accountBalance;
    }

    public void AddCharge(decimal charge)
    {
        _accountBalance += charge;
        if (_accountBalance < 0)
        {
            throw new ArgumentException(
                "The charge cannot put the customer in credit");
        }
    }    
}

.. - Customer, . , _accountBalance , AddCharge.

article .

+4

- , .

, , , , , .

- , . , \ . , , - . , - , .

, , . , -

public class ActionHistory
{
    private string[] _actionHistory;

    public string[] HistoryItems
    {
        get{return _actionHistory; }
        set{ _actionHistory = value; }
    }
}

. [] . , , .

-

public class ActionHistory
{
    private string[] _actionHistory;

    public IEnumerable<string> HistoryItems
    {
        get{return _actionHistory; }
    }
}
+4

(, , ) - direct . / #, .

, , , .

, , , , , , .

:

public class InformationHiding
{
    private string _name;
    public string Name
    {
       get { return _name; }
       set { _name = value; }
    }

    /// This example ensures you can't have a negative age
    /// as this would probably mess up logic somewhere in
    /// this class.
    private int _age;
    public int Age
    {
       get { return _age; }
       set { if (value < 0) { _age = 0; } else { _age = value; } }
    }
}
+1

, , . , "" , , . , Bank TransactionLog ; .

, . , , ( ), , .

+1

?

:

public class Vehicle
{
    private bool isEngineStarted;

    private void StartEngine()
    {
        // Code here.
        this.isEngineStarted = true;
    }

    public void GoToLocation(Location location)
    {
        if (!this.isEngineStarted)
        {
            this.StartEngine();
        }

        // Code here: move to a new location.
    }
}

, isEngineStarted , .. . , Vehicle , , . , , -, : , .

?

, . , . .

, /. , , StartEngine , true this.isEngineStarted. isEngineStarted , true, , StartEngine. isEngineStarted .

+1

, , , . wikipedia :

, .

, , . , , , .

, , , , , .

, , , , .

public class Angle
{

   private double _angleInDegrees;

   public double Degrees
   {
      get 
      {
          return _angleInDegrees;
      }
      set
      {
          _angleInDegrees = value;
      }
   }

   public double Radians
   {
      get
      {   
          return _angleInDegrees * PI / 180;
      }
      set
      {
          _angleInDegrees = value * 180 / PI;
      }
   }
}
0

Data hiding is defined as hiding a base class method in a derived class by naming the new class method the same name as the base class method.

class Person
{
  public string AnswerGreeting()
  {
     return "Hi, I'm doing well. And you?";
  }
}

class Employee : Person
{
   new public string AnswerGreeting()
   {
      "Hi, and welcome to our resort.";
   }
}

In this C # code, the new keyword does not allow the compiler to give a warning that the implementation of the AnswerGreeting base class is hidden when implementing the method with the same name in the derived class. Also known as "hiding inheritance data."

0
source

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


All Articles