What is the use of encapsulation when I'm able to change the property values with setter methods?

前端 未结 15 2045
情深已故
情深已故 2020-12-08 11:08

I try to understand a lot of times but I failed to understand this.

Encapsulation is the technique of making the fields in a class private and provi

15条回答
  •  时光取名叫无心
    2020-12-08 11:38

    Instead of letting everyone access the variables directly:

    public Object object;
    

    Is better to use SET and GET methods, or for example just the GET method (Sometimes you dont want nobody to set other value to that variable).

    public Object getObject() { 
    
    return object;
    }
    
    public void setObject(Object object) { 
    
    this.object = object;
    }
    

提交回复
热议问题