Abstraction in Java?

前端 未结 10 1845
鱼传尺愫
鱼传尺愫 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 00:57

    Abstraction is all about a concept/model which cannot be realized / instantiated as such. Abstraction is all about restriction on an object's method/members behaviour to other classes.

    0 讨论(0)
  • 2021-02-15 01:01

    Personally I wouldn't say encapsulation is really about abstraction (though I see how it could be taken that way), it's about only permitting a user to see or do what's necessary - they only see an interface to the class, not its inner workings. In your case it's achieved because you're only ever setting or getting the name of the particular class, you never access the name variable directly and never see how it's stored. So you could change the name or type of the name variable to something completely different and the interface to your class would still work and look the same. I guess that could be taken in a sense as an abstraction.

    The definitions are loose, but I'd consider polymorphism to fall more into the realms of abstraction, where you decouple the implementation (say, ArrayList) from the interface it inherits (say, List.) That way you just deal with the list interface, and the underlying list could be anything, but that's an implementation detail and because you're an abstract level "above" it, you don't need to worry about it. Of course this is a simplification, sometimes you need to know implementation details for performance reasons or if some operations may not be implemented or allowed on your specific implementation. But from a loose viewpoint (and a pure OO viewpoint) it holds.

    Whatever you understand it to be, the most important thing is you understand the logic behind it, why it's a good idea and why it's always better to do things that way (in this case, have fields as private and use getters / setters to access them.)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-15 01:07

    I think he's confusing polymorphism with encapsulation. Polymorphism can help you achieve abstration.

    0 讨论(0)
  • 2021-02-15 01:10

    It seems encapsulation and abstraction has got everyone confused. If you ask me, those are poles-apart topics and there is absolutely no scope of confusion in this.

    abstraction happens when you use "abstract" keyword, and encapsulation happens when you create a class. a good implementation of encapsulation involves making all your data members private.

    I wrote a few blog posts that might help you:

    Learn how to use an Abstract Class in Object Oriented Design

    The Theory of Abstraction

    0 讨论(0)
  • 2021-02-15 01:10

    Abstraction is done when you want to hide the data.Whereas encapsulation is done when you want to hide both data and code.That is wrapping both data and code which you implement.

    You can implement abstraction by using abstract class or interface. In abstract class we can either write concrete methods or abstract methods but in interface we can only use abstract methods.

    You can implement encapsulation by using access modifiers like public, protected, private. These access modifiers control the access of your data i.e whether it should be public(can be seen by anyone) or protected(can be accessed only by extended classes) or private(hiding it from everyone).

    0 讨论(0)
提交回复
热议问题