Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6762007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:15:42+00:00 2026-05-26T14:15:42+00:00

I have four classes. Class Person , and three more, Student , Professor ,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:15:43+00:00Added an answer on May 26, 2026 at 2:15 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have four classes. Request, DerivedRequest, Handler, DerivedHandler. The Handler class has a property
I have four classes which share some arrangement of four properties. I have currently
I have four classes, let's call S1, S2, S3 and S4. These class are
I have got four classes A , B , C and D . Class
Say I have four sub-classes of 'Car'. One for each color. I want to
I have a four classes: class A(models.Model): field1 = models.BooleanField(default = True) class Meta:
I have the following PHP Classes class.property.php class.location.php class.amenity.php class.category.php all four classes handles
I have four classes: class A {}; class B : virtual public A {};
I have 3 class which derived from each other: class Basic{ ... } class
I have four UINavigationControllers assigned each to a tab in a UITabBarController. Each UINavigationController

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.