Abstract class declaration problems

0

I'm trying to create the next hierarchy. I have an abstract class called person in the following way.

public abstract class Persona {

protected String nombre;
protected String apellidos;
protected final String dni;


public String getNombre() {
    return nombre;
}
public void setNombre(String nombre) {
    this.nombre = nombre;
}
public String getApellidos() {
    return apellidos;
}
public void setApellidos(String apellidos) {
    this.apellidos = apellidos;
}
public String getDni() {
    return dni;
}

of which I need to create a class called partner that inherits from this person class. the problem is that since you can not instantiate from an abstract class I need to do it from the partner class but I miss an error with the final DNI attribute. the code of the partner class is as follows.

package Biblioteca;

public class Socio extends Persona {

public Socio(String nombre, String apellidos, String dni) {

    this.nombre = nombre;
    this.apellidos = apellidos;
    this.dni = dni;

}

}

the case that the DNI attribute has to be final and I can not do it.

    
asked by andres ribeiro 02.11.2018 в 18:15
source

1 answer

0

I think you should create a constructor in the abstract class, something like this:

public abstract class Persona {

protected String nombre;
protected String apellidos;
protected final String dni;

public Persona(String nombre, String apellidos, String dni) {
    this.nombre = nombre;
    this.apellidos= apellidos;
    this.dni= dni;
}

public String getNombre() {
    return nombre;
}
public void setNombre(String nombre) {
    this.nombre = nombre;
}
public String getApellidos() {
    return apellidos;
}
public void setApellidos(String apellidos) {
    this.apellidos = apellidos;
}
public String getDni() {
    return dni;
}
}

And then use it from the constructor of the partner class.

public class Socio extends Persona {
    public Socio(String nombre, String apellidos, String dni) {
        super(nombre, apellidos, dni);
    }
}

I hope it serves you.

Good luck!

    
answered by 02.11.2018 в 21:33