Given:
class PhysicsClass {
private PhysicsInstructor instructor;
private Set<PhysicsStudent> students;
}
class ChemistryClass {
private ChemistryInstructor instructor;
private Set<ChemistryStudent> students;
}
class MathemeticsClass {
private MathemeticsInstructor instructor;
private Set<MathemeticsStudent> students;
}
Is it possible to collect all of these classes in one generic class like:
class ScienceClass<T> {
// What to write here?
// What to write here?
}
If this is not possible, then how can I prevent the code duplication here?
I thought of making it like:
class ScienceClass<T extends Instructor, S extends Student> {
private T instructor;
private Set<S> students;
}
but this would allow me to make something like:
ScienceClass<PhysicsInstructor, ChemistryStudent> scienceClass;
which would be incorrect.