Search code examples
javaobjectsubclasssuperclass

Java, is it possible to 'convert' object from subclass to object from superclass


I have two classes Student and Tutor. Tutor is basically a student (Tutor extends Student) who has facultyID. Once his contract is complete, he returns to being just a student. So can I somehow convert him back to his "previous" roll of student?


Solution

  • What you really want to do here is use composition and not inheritance. Keep all your objects as type Student, and then temporarily assign the behaviour of a TutorRole as it is required to each instance of Student.

    With this design your Student class will contain a property (member variable) of type TutorRole that you can add or remove at runtime. Adding an isTutor() method will allow you to detemine whether a Student is a Tutor at runtime in a clear and concise manner.

    The TutorRole class will encapsulate the behaviour (i.e. methods) of being a Tutor.

    /*
     * The TutorRole can be set at runtime
     */
    public class Student {
    
        private String facultyId;
    
        private TutorRole tutorRole = null;
    
        public boolean isTutor() {
            return !(tutorRole == null);
        }
    
        public void doTutorStuff() {
            if(isTutor()) {
                tutorRole.doTutorStuff();
            }
            else {
                throw new NotTutorException();
            }
        }
    
        public void setTutorRole(TutorRole tutorRole) {
            this.tutorRole = tutorRole;
        }
    }
    
    /*
     * Ideally this class should implement a generic interface, but I'll keep this simple
     */
    public class TutorRole {
    
        public void doTutorStuff() {
            // implementation here
        }
    }
    
    /*
     * Now let's use our classes...
     */
    Student st = new Student(); // not a tutor
    st.setTutorRole(new TutorRole()); // now a tutor
    if(st.isTutor()) {
        st.doTutorStuff();
    }
    st.setTutorRole(null); // not a tutor anymore
    

    An alternative approach is to have a Tutor class contain a reference to a Student object, but it depends on how you are going to be interacting with the Student and Tutor objects on which way around you want to code this.