vedran
vedran

Reputation: 2188

Java, how to make variable of the upper class stays null; how to 'hide' variable from the subclass?

I have four classes. Class Person, and three more, Student, Professor, Tutor, each of which extends class Person. Class Person has 2 variables studentID and staffID. However, only student can have studentID != null, and Tutors and Professors can have staffID != null. Now when creating new object Student, how can I make sure that no matter what staffID always stays null? staffID must remain in class Person, so no moving it around.

Upvotes: 0

Views: 372

Answers (6)

Steve J
Steve J

Reputation: 2674

Create interfaces to restrict operations:

package main;

public interface IsStaff {

    public void setStaffId(Integer staffId);

    public Integer getStaffId();

}



package main;

public interface IsStudent {

    public void setStudentId(Integer studentId);

    public Integer getStudentId();

}

Create your Person class:

package main;

public class Person {

    protected Integer studentId = null;

    protected Integer staffId = null;

}

Create your Student and Staff subclasses with the Interfaces to define the only allowed operations:

package main;

public class Staff extends Person implements IsStaff {

    @Override
    public void setStaffId(Integer staffId) {
        this.staffId = staffId;
    }

    @Override
    public Integer getStaffId() {
        return staffId;
    }

}



package main;

public class Student extends Person implements IsStudent {

    @Override
    public void setStudentId(Integer studentId) {
        this.studentId = studentId;
    }

    @Override
    public Integer getStudentId() {
        return studentId;
    }

}

And now create your Tutors and Professors:

package main;

public class Tutor extends Staff {

}


package main;

public class Professor extends Staff {

}

Objects of the Student class don't have operations that can affect the staffId, and objects of the Staff class (including the Tutor and Professor subclasses) don't have operations that can affect the studentId. Add other operations as necessary (common ones can go into Person directly).

As a bonus, you can use the interfaces to better define methods, like this:

public void assignParkingSpaceTo(IsStaff staffMember);

public void issueLateSlipTo(IsStudent student);

Upvotes: 2

PTBG
PTBG

Reputation: 595

make the variable private, create getters and setters. Then make your constructor initialize both variables, and simple pass null for student ID for teachers, and null for staffID for students.

constructor --->

public Person(int staffID, int studentID){
this.staffID = staffID;
this.studentID = studentID:
}

when you initialize ---->

 Student student = new Person(null, 1234);

Upvotes: 0

DwB
DwB

Reputation: 38300

don't extend the Person class. Use composition instead.

If you want polymorphism, create a Blammy interface that provides a common interface for Student, Professor, and Tutor. The student ID and staff Id stuff could or could not be part of the Blammy interface.

Each class, Student, Professor, and Tutor would contain a private instance of Person and proxy to any Person functionality they wanted to expose.

Student would have a studentId data member and Professor and Tutor would have a staffId data member. The getStudentId() for profesor and tutor would always return null and the get staffId for student would also always return null.

Upvotes: 0

Max
Max

Reputation: 842

Make this variables private and create getter and setter. Depending of class implement different methods behaviors. For stasfs - protect setting studentId for student - protect setting staffId.

Upvotes: 0

jpredham
jpredham

Reputation: 2199

The studentID should be a member of Student not Person. Likewise Professor and Tutor should both extend a class Faculty which has a staffId which in turn extends Person

Upvotes: 1

Polynomial
Polynomial

Reputation: 28316

The whole point of inheritance is that only values and functions (members) that are valid for a parent class are placed in the parent class, and any child-specific members are placed in the child class.

Don't abuse OO, move staffID into your child class.

My suggestion would be to create a Staff class that contains staffID, have it inherit Person, then have Professor and Tutor inherit from it. Then move studentID into the Student class. That way only Professor and Tutor have access to staffID and Student does not, and vice versa for studentID. All classes can still be assigned to a Person type.

Upvotes: 5

Related Questions