Are there any reasons to have an abstract class with every method in the class defined?

后端 未结 7 700
名媛妹妹
名媛妹妹 2021-02-05 01:42

It seems that an abstract class means the definition of the class is not complete and hence cannot be instantiated.

And I saw some simple Java code which has an abstract

7条回答
  •  时光取名叫无心
    2021-02-05 02:22

    One typical use case is the creation of an adapter class. Think of a callback interface where you could be notified of 10 different events but are normally only interested in some of them. With an adapter class, you can provide empty implementations such that an actual callback only needs to implement those methods that are of interest after extending the adapter. By making the adapter abstract, you express the fact that it makes no sense to instantiate the adapter itself as it does nothing useful.

    Since Java 8, you would not longer implement such an adapter but use default methods for the interface.

提交回复
热议问题