You can only inherit single class in java, you can't have class WorkingStudent extends Student, Staff
. But you can implement multiple intefaces, so i will propose you a solution with interfaces. First let's define the student functionality. A student has to study, so:
public interface Student {
void study();
}
Now for the staff, he has to work obviously, so:
public interface Staff {
void work();
}
A very simple person class:
public class Person {
private final String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
The average student just studies, or not, who knows, but let's assume he does. He is a Person, and also does Student stuff.
public class NormalStudent extends Person implements Student {
public NormalStudent(String name) {
super(name);
}
@Override
public void study() {
System.out.println(getName() + " studies a lot.");
}
}
And your average staff, who does not do his job very dutyfully. He is a Person, and he does Staff thingies.
public class NormalStaff extends Person implements Staff {
public NormalStaff(String name) {
super(name);
}
@Override
public void work() {
System.out.println(getName() + " does whatever the staff does");
}
}
Now for the student, who is also a staff member:
public class WorkingStudent extends NormalStudent implements Staff {
public WorkingStudent(String name) {
super(name);
}
@Override
public void work() {
System.out.println(getName() + " has finished studying, but does not go to the party and instead does his obligations as a staff member of his university.");
System.out.println(getName() + " has a bright future ahead of him. Probably.");
}
}
WorkingStudent is a NormalStudent, also a Person(he inheriths the functionality for a Student from NormalStudent and the properties of a Person), and implements Staff thus receiving the corresponding functionality.
And a small example:
NormalStudent james = new NormalStudent("James");
james.study();
WorkingStudent george = new WorkingStudent("George");
george.study();
george.work();
NormalStaff john = new NormalStaff("John");
john.work();