2

Problem statement: A learning environment contains both students and staffs who have attributes in common because they are both people. In some institutions a student may also be working as a staff there at the same time. The implications of the above stated in Object Oriented Programming is:

  • Student inherits Person
  • Staff inherits Person
  • In some instances, a Student object and a Staff object should both be built on the same Person instance(I.e share the same parent class instance)

How do I implement the last implication on the list using OOP? (C#, Java, C++, or Python preferably)

Joe
  • 41
  • 5
  • 4
    you don't. each instance is an instance on itself. What you are describing is composition, not inheritance – Stultuske Jan 18 '22 at 12:38
  • Are the implications you listed specified in the problem statement or are these your own interpretation ? – Angela Lopez Jan 18 '22 at 12:39
  • @Angela Lopez The first two are based on the traditional rules of OOP. The 3rd is how I personally think the described seinario should be implemented, but I'm open to other suggestions. – Joe Jan 18 '22 at 12:50

3 Answers3

3

I don't think that inheritence is the right solution to your problem. Is it necessary that Student and Staff are different classes?

It seems that it would be more appropriate that a student or a staff member are just instances of Person, and there is a member property role which may contain "Student" or "Staff" or both.

Karsten Gabriel
  • 3,115
  • 6
  • 19
0

With interface IPerson(interpreting inherit a litle broader) you could do somithing like this in Java (it's close but possibly no cigar):

public interface IPerson
{

    default String getName() {
        return getPerson().getName();
    };

    default void setName(String name) {
        getPerson().setName(name);
    }

    IPerson getPerson();

}

public class Person implements IPerson
{

    private String name;

    @Override
    public String getName()
    {
        return name;
    }

    @Override
    public void setName(String name)
    {
        this.name = name;

    }

    public IPerson getPerson() {
        return this;
    }
}

public class Staff implements IPerson
{

    private Person person;

    private String socialInsuranceNumber;

    public String getSocialInsuranceNumber()
    {
        return socialInsuranceNumber;
    }

    public void setSocialInsuranceNumber(String socialInsuranceNumber)
    {
        this.socialInsuranceNumber = socialInsuranceNumber;
    }

    public Staff(Person person)
    {
        super();
        this.person = person;
    }

    @Override
    public IPerson getPerson()
    {
        return person;
    }

}

public class Student implements IPerson
{

    private Person person;

    private String matriculationNumber;

    public String getMatriculationNumber()
    {
        return matriculationNumber;
    }

    public void setMatriculationNumber(String matriculationNumber)
    {
        this.matriculationNumber = matriculationNumber;
    }

    public Student(Person person)
    {
        super();
        this.person = person;
    }

    @Override
    public IPerson getPerson()
    {
        return person;
    }

    public static void main(String[] args) {
        Person peterP = new Person();
        peterP.setName("Peter");

        Staff peterStaff = new Staff(peterP);
        peterStaff.setSocialInsuranceNumber("123");

        Student peterStudent = new Student(peterP);
        peterStudent.setMatriculationNumber("456");

        peterStudent.setName("Dr. Peter");

        System.out.println(peterStaff.getName());

    }

}

Without using an interface you could override all getters, setters and alike and delegate it to the Person(ignoring the fields in the subclass, not nice either. This is composition disguised as inheritence):

public class Student extends Person
{

    private Person person;

    private String matriculationNumber;

    public String getMatriculationNumber()
    {
        return matriculationNumber;
    }

    public void setMatriculationNumber(String matriculationNumber)
    {
        this.matriculationNumber = matriculationNumber;
    }

    public Student(Person person)
    {
        super();
        this.person = person;
    }

    @Override
    public String getName()
    {
        return person.getName();
    }

    @Override
    public void setName(String name)
    {
        person.setName(name);
    }

}
Turo
  • 4,724
  • 2
  • 14
  • 27
0

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();
Chaosfire
  • 4,818
  • 4
  • 8
  • 23