Java abstract class fields override

后端 未结 6 944
春和景丽
春和景丽 2020-12-06 11:34

I have an abstract class that should implement a public field, this field is an interface or another abstract classe.

something like this:

public abstr         


        
6条回答
  •  情书的邮戳
    2020-12-06 12:15

    Regarding

    ....and i IMAGINE that the field "child" in SpecialContainer is automatically overloading the field 'child' of the GenericContainer...

    No. Fields don't get overridden, only methods do.

    This is one reason why use of (overridable) getter and setter methods are preferred to direct access to fields. Your fields should almost all be private.

    As for your design, there's no need for your SpecialContainer class to have a SpecialChild field, but instead the SpecialChild object should be placed in the GenericChild field.

提交回复
热议问题