What is Encapsulation?

Encapsulation is one of the fundamental units of object-oriented programming. It simply means that putting all the data at one place, Binding And Wrapping of data and methods together is called Encapsulation.

Through encapsulation we can achieve data hiding, if you are familiar with any object oriented programming language then you must be familiar with getters and setters method. When we try hide the data then we use “private” access modifier to stop the any kind of modification of attributes. Now the question arises if we have stopped any modification then how will we change or get the value of elements, so for that only getters and setters methods are used.
Getter method is used to return the value of that element and setter method is used to put the value in that element.

Now lets understand it with an example:
private int price;
Here the price will not be accessible outside of the class, but we can set the data through set method,
图片[1]-What is Encapsulation? - 拾光赋-拾光赋
and now we can get the data through get method,
图片[2]-What is Encapsulation? - 拾光赋-拾光赋
}

原文链接:What is Encapsulation?

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容