M
M
Maxim Isaev2021-02-22 16:19:03
Java
Maxim Isaev, 2021-02-22 16:19:03

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

3 answer(s)
O
Orkhan, 2021-02-22
@MaximIs

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 {}

So, you say that there is an Университет
Факультет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.
Next you say that Студентextends the Group class. It is also illogical, since each group contains a certain number of students.
The following implementation would be more correct:
public class University {
private List<Faculty> faculties;
}
public class Faculty{
private List<Group> groups;
}
public class Group{
private List<Student> students;
}
public class Student {}

Moreover, instead of List, you can use Set, since each faculty is unique, as well as the group and students.

D
Denis Zagaevsky, 2021-02-22
@zagayevskiy

Inheritance is not needed here at all, aggregation is needed.

I
illuzor, 2021-02-22
@iLLuzor

No, not true.
Because you have a faculty, a group and a student - everything is a university.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question