c# - what is the meaning of data hiding -
one of important aspects of oop data hiding. can explain using simple piece of code data hiding , why need it?
i'm guessing data hiding mean encapsulation or having variable within object , exposing , modify methods, when want enforce logic setting value?
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 customer in credit"); } } } i.e. in example, i'm allowing consuming class balance of customer, i'm not allowing them set directly. i've exposed method allows me modify _accountbalance within class instance adding via charge in addcharge method.
here's article may find useful.
Comments
Post a Comment