Abstraction in Java?

前端 未结 10 1851
鱼传尺愫
鱼传尺愫 2021-02-15 00:43

Today i heard from my friend, that encapsulation is not only achieving information hiding but also abstraction. How does it achieve?

public class employee {

           


        
10条回答
  •  南方客
    南方客 (楼主)
    2021-02-15 01:04

    but where does the abstraction come into picture here?

    you've said it yourself: "allowing the class to access my public method rather than private members" or in other words: allowing other classes to access what they may access, and protecting what they may not.

    the abstraction here comes from the public methods, for instance in getName() you don't need to always give the private member value, it could be appended with other value or even it could give totally different thing. it's like saying: "tell me your name, regardless how you'd give it to me". maybe a better example would be a method named getYourWorkDone(). the principle remains the same: "get your work done! how? I don't care how!"

    the encapsulation part is from the private members. this class encapsulates those private members so they are grouped to form the class' state.

提交回复
热议问题