Abstraction in Java?

前端 未结 10 1822
鱼传尺愫
鱼传尺愫 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: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).

提交回复
热议问题