2
votes

This question was asked by the interviewer but I am not able to get answer of it anywhere else.

Question

I have School Class, Student Class and Academy Class. I School Class has all the Student Information but I want to pass List of Students with the relevant information to Academy class.

For. e.g. Student Class has 80 fields but we should only pass 5 fields and restrict every other fields while passing the list of students to Academy.

My answer to this was that we can create StudentDummy Class which will extend Student class and will override getter methods of those fields which we don't want to show and it will return message saying "This property is restricted" and then send List of StudentDummy Objects instead of student objects. For this he said if the fields are multiple then will have to override many getters. Can you please tell me proper Object Oriented Approach of this?

2
The requirements are not clear enough IMO. What are the overall fields of Student and why is only a subset allowed to be seen by Academy? Why does School "has all the information" instead of the instances of Student themselves; what does that mean? What are the functions in question here and what is their logic; what are you trying to model exactly?akuzminykh
Often there is no single correct answer to an interview question, because the interviewer is not interested in what you've memorized, but rather in how you think. Satisfying interviewers is a matter of interacting with them to clarify requirements, understand what they are looking for, and talk through solutions; because ultimately it's their opinion of your answer that matters, whether your answer is correct or not.jaco0646

2 Answers

1
votes

I think in this case you should create interface with abstract methods relevant to Academy. Student class must implement this interface. And then create List of interface type.

0
votes

If you need to create an object and exclude some fields, you can create an overloaded constructor