Java Encapsulation Concept not clear

后端 未结 12 2397
死守一世寂寞
死守一世寂寞 2020-11-30 03:40

This is basic question but still i don\'t understand encapsulation concept . I did\'t understand how can we change the properties of class from other class.because whenever

12条回答
  •  星月不相逢
    2020-11-30 04:05

    Encapsulation means combining data and code together(class). The main purpose of encapsulation is you would have full control on data by using the code.
    
    class Encap{
    
    private int amount;
    
    public void setAmount(int amount)
    {
    this.amount = amount;
    }
    
    Here, you can set the amount using the setAmount method, but value should be more than 100. So, i have the control on it.
    
    public void setAmount(int amount)
    {
    if(amount>100)
    this.amount = amount;
    }
    

提交回复
热议问题