Answer the question
In order to leave comments, you need to log in
Have I specified inheritance in the Java project correctly?
Hello.
Can you please tell me if I implemented the chain of inheritance correctly?
public class University{}
public class Faculty extends University {}
public class Group extends Faculty {}
public class Student extends Group {}
If it's not difficult, explain what is wrong. Thanks
Answer the question
In order to leave comments, you need to log in
Not true.
It is easier to understand your mistake if you speak the code you wrote in words.
Here is your code:
public class University{}
public class Faculty extends University {}
public class Group extends Faculty {}
public class Student extends Group {}
Университет
Факультет
EXPANDS University. Already a mismatch. After all, there are faculties at the University. Next, you say that Группа
expands the Faculty. Which is also illogical, because each faculty INCLUDES groups. Студент
extends the Group class. It is also illogical, since each group contains a certain number of students. public class University {
private List<Faculty> faculties;
}
public class Faculty{
private List<Group> groups;
}
public class Group{
private List<Student> students;
}
public class Student {}
Inheritance is not needed here at all, aggregation is needed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question