How to access the private variables of a class in its subclass?

前端 未结 28 2377
清歌不尽
清歌不尽 2020-12-17 09:46

This is a question I was asked in an interview: I have class A with private members and Class B extends A. I know private members of a class cannot be accessed, but the qu

28条回答
  •  感动是毒
    2020-12-17 10:24

    Obviously, making them protected, or adding setters/getters is the preferred technique. Reflection is a desperation option.

    Just to show off to the interviewer, IF "access" means read access, and IF Class A generates XML or JSON etc., you could serialize A and parse the interesting fields.

提交回复
热议问题