How to get around the lack of abstract fields in Java?

前端 未结 5 2068
遇见更好的自我
遇见更好的自我 2021-01-13 01:06

Assume, we have an abstract class A and we want to force all subclasses to have a certain field. This is not possible in Java, because we can not define abstrac

5条回答
  •  甜味超标
    2021-01-13 01:53

    I think opt.1 is the cleaner by far. A few getters and setters is not a big deal, and I doubt that many use cases would have more than just a few abstract "fields".

    About opt.2, you forget that constructors are not inherited, and thus would require all sub classes constructors to be implemented in a way that takes a into account.

提交回复
热议问题