protected data in abstract class

前端 未结 8 1693
闹比i
闹比i 2021-01-01 19:01

My question involves specifically Java, abstract classes, and the use of protected data. I am being told that all the data should be private, and protected getters/setters

相关标签:
8条回答
  • 2021-01-01 19:46

    Think of protected methods as an interface for subclasses, in the same way that public methods are an interface for everyone else.

    Providing accessors enables the base class to maintain its state: there's no way a subclass would corrupt it without an intentional trick.

    0 讨论(0)
  • 2021-01-01 19:48

    If the field is private and access is through getters and setters, you will be able to reimplement getters and setters (for instance, dropping the field and updating/reading the value from an external source), and thus change how the "field" works without touching any child classes.

    Whether this is worth it, that's up to you.

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