Currently I have admin class, user class and lecturer class. Then I want user and lecturer inherit from user. But the problem is my user class constructor is about the username, password, but the admin and lecturer class constructor I put it with the their profile such as their name, age, gender all those things? Thus, lecture and admin are different with the user class. So is I have something wrong in this?
Asked
Active
Viewed 105 times
0
-
1I'm not quite sure what the question here is. If you're asking "will my code compile", just try it and see. If you're asking "my code doesn't work, how do I fix it", we're going to need to see a [mre] in order to help you. – Charlie Armstrong Sep 01 '20 at 04:59
-
1@CharlieArmstrong He's asking more of a class modeling question. – Amir Afghani Sep 01 '20 at 05:01
-
I just quite confused for the inheritance when I write the program – Sep 01 '20 at 05:02
-
@AmirAfghani I got that, I was just struggling to understand what the issue was. The English in the question is a bit difficult to understand, at least for me, and I thought some code might clarify things. I still do think it would clarify the question, if the OP added it for future readers. – Charlie Armstrong Sep 01 '20 at 05:16
-
Your description sounds fine, you just need to use `super( username, password)` as the first line of your `Lecturer` and `Admin` constructors if you don't want to make the no-arg constructor public. You really should include your code though. – matt Sep 01 '20 at 07:42
3 Answers
1
If you want define class Admin (admin) and Lecturer (lecturer) as User (user), then you can let class Admin and Lecturer inherit from User. But if class Admin and Lecture are not suppose to depend on "username, password", then there is no point in extend from User class. Here is an alternative:
public interface User {
}
public class UserImpl implements User {
private final String username;
private final String password;
public UserImpl(String username, String password) {
this.username = username;
this.password = password;
}
}
public class Admin extends UserGroupAdmin implements User {
protected Admin(String name, int age, String gender) {
super(name, age, gender);
}
}
public class Lecturer extends UserGroupAdmin implements User {
protected Lecturer(String name, int age, String gender) {
super(name, age, gender);
}
}
Make an extra abstract class for common parts:
public abstract class UserGroupAdmin {
private final String name;
private final int age;
private final String gender;
protected UserGroupAdmin(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
}
Extra reading when it comes to when and when not to inherit: "Liskov Substitution Principle": What is an example of the Liskov Substitution Principle?

DigitShifter
- 801
- 5
- 12
0
There is more than one way you can accomplish your goal. Here's one :
//base class
abstract class User {
User(String name, int age, ...) {
}
}
class AuthenticatedUser extends User {
AuthenticatedUser(String name, int age..., String username...) {
super(name, age, ...);
//authentication setup next
}
}
class SystemUser extends User {
//etc etc
}

Amir Afghani
- 37,814
- 16
- 84
- 124
0
I Guess you want to inherit the user properties to both admin and lecturer class. So you need to extend the Base class user here.
public class User {
private String username;
private String password;
public User(){
}
}
class Admin extends User{
private String name;
private Integer age;
private String gender;
public Admin(){
// Constructor code here
}
}
class Lecturer extends User{
private String name;
private Integer age;
private String gender;
public Lecturer(){
// Constructor code here
}
}

Shubham Jain
- 21
- 3
-
Although admin and lecturers didn't have the use of username and password, we also can let the admin and lecturer inherit from user? – Sep 01 '20 at 05:10
-
If they don't have anything to inherit from User, then I wouldn't inherit from them. Inheritance is a "isa" relationship. Unless you can state that A is a B, then A should not inherit from B, generally. – NomadMaker Sep 01 '20 at 05:17
-
I would want to pass the initial values through the constructors as indicated in the question. I would also think twice before duplicating identical fields in the two subclasses rather than moving them into the superclass. – Ole V.V. Sep 01 '20 at 05:45