Search code examples
javaclasssubclasssuperclass

Trouble with Sub-Classes and Inheritence


So imagine I have a Superclass Staff which has generic variables such as Name, address etc. There are 4 subclasses Doctor, Nurse, Receptionist and OfficeManager each with unique variables and methods. The problem with this set up is, a Doctor can also be an OfficeManager for example, so would need access to the variables and methods in OfficeManager Is there a way an instance of Doctor can have access to methods in OfficeManager? Is having Staff as a superclass and the 4 subclasses an ideal design?

Any other ideas would be appreciated.

ps. I haven't started drawing up the code as I still don't know the design stage, ie Classes.


Solution

  • If several subclasses need the same functionality, that functionality should be moved either to the Staff class or some level between Staff and e.g. Doctor. It depends on the functionality and intent. The subclasses should not be aware of each other though, that kind of ruins the whole idea.