How to inherit from two classes

I have the People and Student and Employee classes that inherit it. But if I have a person who is Student and Employee ...

... how would you do that?

+6
source share
5 answers

This is a classic example of a problem area that has not been properly analyzed. Yes, in some situations it may be right to think of “Student” as a type of “Face” and “Employee” as a type of “Face”, but - depending on your area of ​​concern - it may also be inappropriate.

If your domain requires something to be an “apprentice” and an “employee”, you should consider whether the relationship between the “apprentice” and the “person” in your problem area is really “is-a”.

It is possible that in this particular case, being a student is only an attribute of a particular person. So, John Doe is a person who can also have a student’s “current activity”. In this case, he may have a list of several "current activities". And relationships in such a world become a has-a, not an is-a. So, “Student” and “Employee” become subclasses of “Occupation”.

+15
source

According to the comment:

This is an interview question, I think you can make any guess here

If so, the only correct answer is to describe your assumptions and how to solve them.

If you strictly adhere to the requirements, you can make the Student and Employee interfaces and implement different classes:

 public interface Student { void learn(); } public interface Employee { void work(); } public class Person { // properties, getters and setters for name, age, sex, etc. } public class StudentPerson extends Person implements Student { @Override public void learn() {} } public class EmployeePerson extends Person implements Employee { @Override public void work() {} } public class StudentEmployeePerson extends Person implements Student, Employee { @Override public void work(); @Override public void learn() {} } 

Taking the extra mile, you need to extract the logic work() and learn() in the helper classes and call StudentPerson , EmployeePerson and StudentEmployeePerson respectively.

But this, IMHO, missed the point of the exercise.

A student who also has a job is still a student. It cannot be a separate class for a student who does not.

I would create a Role interface, have Student and Employee implement it, and allow Person have multiple Role s, so it can be both a student and an employee:

 public interface Role { void perform(); } public class Student implements Role { @Override void perform() { /* go study */ } } public class Employee implements Role { @Override void perform() { /* go work */ } } public class Person { // properties, getters and setters for name, age, sex, etc. private Set<Role> roles = /* initialized somehow */ public void doStuff() { for (Role role : roles) { role.perform(); } } } 

EDIT:
To answer the question in the commentary, a person who is both a student and an employee will be built by adding Role s. For simplicity, the following snippet assumes that these classes have constructors from the corresponding properties and that Person has methods for adding and removing Role to the internal set:

 Person person = new Person ("John Doe", 21, Sex.Male); person.addRole (new Student ("StackOverflow University")); person.addRole (new Employee ("Secret Government Spy")); 
+11
source

Since Java does not allow multiple inheritance, you should define interfaces for Student and Employee instead of inheriting them as base classes.

+4
source

What you can do, you can change the public class Employee to the public interface Employee , and then you can do the public class Student extends People implements Employee

You will have to work this way because in java one class can inherit from only one class maximum. Greetings.

+4
source

In Java, you can only extend one superclass. However, Java offers interfaces, and one class can extend multiple interfaces.

You can organize your class structure as follows:

 interface People interface Student extends People interface Employee extends People 

Then your student students can:

 class EmployingStudent implements Student, Employee 

In case People is a class that cannot be changed, you can do:

 interface Student interface Employee class EmployingStudent implements Student, Employee 

So the only difference would be that Student and Employee would not distribute people then.

0
source

Source: https://habr.com/ru/post/981566/


All Articles