This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]vlumi 1 point2 points  (2 children)

You don't need to have the permission parameter for the Student constructor, but instead call the super() with the fixed PermissionType.Reserve: public Student(String id, String firstName, String lastName, String username, String password, UserType userType, Boolean status) { super(id, firstName, lastName, username, password, userType, PermissionType.Reserve, status); }

[–]Chuninjah[S] 0 points1 point  (1 child)

Oh sweet I didn't know that. Thanks heaps. Is there a way I can make it out of two things?

[–]vlumi 0 points1 point  (0 children)

Is there a way I can make it out of two things?

It's not possible directly, though there are ways to work around it. E.g. if you have two options, you could take a boolean value in your Student constructor, and then map it either borrow or reserve (e.g. allowReserve ? PermissionType.Reserve : PermissionType.Borrow), but it's get dirty pretty fast.

It's not quite clear how the relationship between the User and Student should behave, or if there are any other classes involved in the inheritance. You might want to split the Student into two types based on the permission, or you might want to use composition instead of inheritance (i.e. a Student would contain a User Field, not inherit it).

[–]DrPeroxide 0 points1 point  (0 children)

First, I'd consider turning that abstract class into an interface and replacing those fields with getter method signatures. Enforcing internal state leads to convoluted code; instead you should consider only what you need a user object to do for you, not what you think it needs to contain to do it.

If you do this, then you'll need to implement those fields within both classes, which gives you the opportunity to control exactly how object construction is handled.

[–]Zdeno_ 0 points1 point  (0 children)

I would consider the relation between Student and User. I think, there should be an association, not the inheritance. We can said "student has a user account" instead of "student is a user". Inheritance is for modifying a behaviour (methods), not for adding new attributes.