Hello!
I have several JAVA-Interfaces: a QUESTION and an ANSWER. The QUESTION-Interface is extended by two more interfaces: NumericQUESTION and TextQUESTION. The ANSWER-Interface is extended by the interfaces NumericANSWER and TextANSWER.
Each Interface is implemented by a Class with Name [InterfaceName]+DTO, i. e. QUESTION is implemented by QuestionDTO. The "DTO" stands for "Data Transfer Object".
Now I want to express that each subclass of AnswerDTO, that is NumericAnswerDTO and TextAnswerDTO, knows about its corresponding question. So NumericAnswerDTO has an attribute "question". The type of the attribute is the one of the interface! Here is a code example:
Code:
public class TextAnswerDTO extends AnswerDTO implements TextAnswer {
//... some inherited attributes
// Now the linkage to the corresponding question
TextQuestion question;
// ... Constructors
public void setQuestion(TextQuestion q) throws AnException {
this.question = q;
}
public TextQuestion getQuestion() throws AnException {
return question;
}
}
The relation between answers and questions is many-to-one, which means that many answers are related to one question.
How can I map this scenario? The classes that are obviously persistet to the database are the DTOs, I use the interfaces to add a level of abstraction, so I can later change the underlying implementation. But it is very important, that the attributes of the DTO-classes holding the reference to a corresponding question are of the interface type!
Thanks for all help in advance!
Philipp Hinnah (Germany)