简体   繁体   中英

Java: Direct constructor in abstract class

I'm trying to set up an abstract class with 6 variables, and a constructor inside that class, that would populate these fields for every new created object.

What I have now is

public abstract class AbstractUser {

    private String username;
    private String password;
    private String fullName;
    private String email;
    private char sex;
    private Date birthday;

    public AbstractUser(String username, String password, String fullName, String email, char sex, Date birthday){
        this.username = username;
        this.password = password;
        this.fullName = fullName;
        this.email = email;
        this.sex = sex;
        this.birthday = birthday;
    }
}

But I am getting the error "The type AbstractUser is already defined".

I would like that it populates the fields with data set from another concrete class, like

AbstractUser User1 = new AbstractUser("John", "john567", "John Evans", "john@msn.com", "m", 123456789);

Is that even possible with an abstract class?

You can't initialize an abstract class. You need to extend your abstract class.

我们可以在Abstract类中使用构造函数,但是我们不能创建抽象类的对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM