Abstraction and abstract in java

末鹿安然 提交于 2019-11-29 07:03:24

"Abstract" is an antonym of "concrete". With abstractions you represent notions and ideas, rather than the concrete way these ideas are implemented. This fits into your understanding of abstraction - you are hiding the details and you only show the interface.

But this also fits with abstract classes - they are not concrete (they can't be instantiated, for one), and they don't specify implementations. They specify abstract ideas that subclasses have to take care of.

So it is basically a different point of view - one is from the point of view of clients of the API, and the other is also about subclasses. (Note that you can use abstract classes instead of interfaces in some cases to achieve the same effect, although it's not considered good practice)

  1. The abstract classes defines an interface which the users of the class will use. An abstract class is similar to an interface except that some method can be implemented and all the abstracts ones will be implemented by the concrete classes that extends it. In summary the advantage is that you can have multiple implementations of the same abstract class that are completely interchangeable because the class that user operate with, is of the abstract type rather than of a specific implementation type.

  2. Using factory methods is an common approach for abstraction but you can also instantiate concrete class with their constructors. The important thing is the type of the variable that must be defined as the abstract type. Doing so the object variable can be accessed only with the interface defined by the abstract class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!