-2

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?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
CodeIsLaw
  • 307
  • 2
  • 13
  • 1
    Please clarify _Now I can't use for example admin.setUsername("admin1");. Why is this?_. Refer to [How to Ask a Good Question](https://stackoverflow.com/help/how-to-ask) and provide [Minimal, Complete, and Verifiable Example (MCVE)](https://stackoverflow.com/help/minimal-reproducible-example). – Ivo Mori Jul 11 '20 at 12:13
  • _As a hint_ – If `class A` extends `class B` then an instance of class `B` can access `A`'s methods as long as their method access modifier allow it... I suggest you consult one of the many tutorials linked under the [`java` tag-info page](https://stackoverflow.com/tags/java/info) regarding inheritance / sub-typing. – Ivo Mori Jul 11 '20 at 12:24
  • I can’t figure out from the information you have provided. In general you should be able to call `admin.setUsername();`. (1) Could you [create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), please? (2) What error message are you getting when you try? – Ole V.V. Jul 11 '20 at 12:26
  • (1) It’s confusing that you create a `User` and call the vairable `admin`. Did you mean to create an `Admin`? (2) Don’t repeat the instance variables in the subclass. The point is that they are inherited from the superclass, so an `Admin` already has got them. – Ole V.V. Jul 11 '20 at 12:28
  • Does this answer your question? [Are fields initialized before constructor code is run in Java?](https://stackoverflow.com/questions/14805547/are-fields-initialized-before-constructor-code-is-run-in-java) – Ole V.V. Jul 11 '20 at 12:34
  • Is admin supposed to be a normal user, or an administrator? Perhaps you meant 'User admin = new Admin(username, fName, nName, password, 1);'? – NomadMaker Jul 11 '20 at 12:47
  • 1
    Please clarify. The problematic setter is not in the code you posted -> we cannot examine the problem. (stating the obvious: if you don't have a setter, you can't use it) . – kai Jul 12 '20 at 02:07

3 Answers3

2

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.

Kntkat
  • 36
  • 3
1

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...

Jon Seeb
  • 11
  • 1
  • You mean well – however trying to answer an unclear question is a guessing game. It's better to ask for clarification in the question's comments and wait for the OP to provide more details, and clarify the problem ;) – Ivo Mori Jul 11 '20 at 12:30
1

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161