Constructor and Getter- and Setter Methods

I already did some research about this topic (e.g. Difference between constructor and getter and setter). The main difference between getter- and setter-methods and constructor is clear for me. I just want to create a simple login window (without a db in background) but I have some issues with my getter and setter methods.

I created a class called user and a class called admin. The user class has a constructor and getter and setter methods:

public class User {          private String fName;     private String nName;     private String password;     private String username;     private int group;      //Constructor     public User(String username, String fName, String nName, String password, int group){          this.username = username;         this.fName = fName;         this.nName = nName;         this.password = password;         this.group = group;     }  //getter and setter methods here  

In an other class I tried to create a new user:

public class Admin extends User{      private String username = "admin";     private String fName = "name";     private String nName = "secondName";     private String password = "password";     private int group = 1;      public Admin(String username, String fName, String nName, String password, int group){         super(username, fName, nName, password, group);     }      User admin = new User(username, fName, nName, password, 1);  } 

Now I can’t use for example admin.setUsername("admin1");. Why is this?

Add Comment
3 Answer(s)

You actually can do that just make sure your getters and setters are public,an easy way to ensure your getters and setters are correct is to right click in your class->source->generate getters and setters then choose which attributes you want to be read and write, However if your intention with this line

    User admin = new User(username, fName, nName, password, 1); 

is to create a new admin with these attributes

username = "admin"; fName = "name"; nName = "secondName"; password = "password"; group = 1; 

maybe try instead:

either passing the values to the constructor directly without the variables

      User admin = new User ("admin","name","secondName",password,1); 

or if you want for these to be the default values that an admin object is to have every time you create it then you can do this in the constructor:

     public Admin (){         super("admin","name","secondName",password,1); 

then

    Admin admin = new admin(); 

then you can change the username like you want and if you want to check that it changed use

    System.out.println(admin.getUsername()); 

in both cases you don’t really need to create the instance variables as they are inherited from user class.

Add Comment

The code snippets are lacking information. How do your getter and setters look like? Are they private or public methods? Did you accidently put them as static methods?

I would not put code below your constructor without putting it into either a constructor or any other method… I don’t know why you want to create instance of inherited method in your class, but if you really want to, do it like this.

public class Admin extends User{  private String username = "admin"; private String fName = "name"; private String nName = "secondName"; private String password = "password"; private int group = 1; private User admin = new User(username, fName, nName, password, 1);  public Admin(String username, String fName, String nName, String password, int group){     super(username, fName, nName, password, group); }  }  

I would recommend you make sure your getters and setters are public, and you should create a logic class seperated from entity and create an admin user there, for ex.:

public class MyService{       public void setSomeStuffForAdmin(){          Admin admin = new Admin();          admin.setUsername("admin1");          // Further logic here...      } }  

Please be more clear or specify what you want to achieve, it is hard to know what the problem is with just a chunk of code not too much related to the issue…

Answered on July 16, 2020.
Add Comment

It’s not exactly what you asked, but I want to mention a couple of problems that I think you will want to solve before caring about how to use admin.setUsername("admin1");.

When creating an Admin object, you are creating an object with 3 (three) sets of fields. Three user names, three passwords, etc. You don’t want that. You will want to reduce to one set.

As you have currently coded you Admin it inherits one set of fields (instance variables) from User, the superclass. Then it has one set of fields declared in the Admin class, that’s two so far. Finally the Admin contains a User object called admin. This object contains a third set of fields (though not initialized for reasons explained in Kntkat’s answer).

I suggest that you make do with the fields in User and delete those fields in Admin that repeat fields from User. And I suggest that you do not create a User object inside the Admin class since an Admin already is a User by inheritance. So also delete the admin field.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.