Polymorphism and Inheritance in Java

0

They commissioned me a job in which I must use the polymorphism and inheritance of POO, the problem is that of 4 "values" that I want to show only shows me 2, I would like to know if someone could give me an alternative for this, not It is something that I consider difficult but nevertheless it does not come out hahaha.

The program is 1 main class, 1 parent class with 2 subclasses. In the main I only instantiate objects and show. In the Father class called "Person" I write the attributes of a person with a constructor that has 3 parameters (name, surname and age) and I have a method that returns my name and surname. Then I have the Master class that inherits name, surname and age but also adds "employee key", it is the only thing in this class. Finally I have the student class that is similar to the master class but instead of adding the key I filled the "enrollment". What I want to get to is, I want my show to show me something like that.

output :

Juan Perez 30 JPMN Gerardo Martinez 18 123123

but running the program just shows me something like that

output :

JuanPerez GerardoMartinez

added the codes :

Main

package lab1; import java.util.ArrayList; import lab1.core. *;

public class Lab1 {


    public static void main(String[] args) {

        ArrayList<Persona> personas = new ArrayList();

        Persona maestro = new Maestro("Juan", "Perez", 30, "JPMN");
        Persona alumno = new Alumno("Gerardo", "Martinez", 18, 123123);

        personas.add(maestro);
        personas.add(alumno);

        for(Persona persona:personas){
            System.out.println(persona.obtenerInformacion());
        }   
    }   
}

Person

public class Persona {

    private String nombre;
    private String apellidos;
    private int edad;

    public Persona(String nombre, String apellidos,int edad){
        setNombre(nombre);
        setApellidos(apellidos);
        setEdad(edad);
    }

    public void setNombre(String nombre){
        this.nombre=nombre;
    }

    public String getNombre(){
        return nombre;
    }

    public void setApellidos(String apellidos){
        this.apellidos=apellidos;
    }

    public String getApellidos(){
        return apellidos;
    }

    public void setEdad(int edad){
        this.edad=edad;
    }

    public int getEdad(){
        return edad;
    }

    public String obtenerInformacion(){
        String bot= nombre + apellidos;
        return bot;
    }   
}

Teacher

public class Maestro extends Persona{

    private String claveEmpleado;

    public Maestro(String nombre,String apellidos,int edad,String claveEmpleado){
        super(nombre,apellidos,edad);
        this.claveEmpleado=claveEmpleado;
    }

    public void setClaveEmpleado(String claveEmpleado){
        this.claveEmpleado=claveEmpleado;
    }

    public String getClaveEmpleado(){
        return claveEmpleado;
    }
}

Student

public class Alumno extends Persona{

    private int matricula;

    public Alumno(String nombre,String apellidos, int edad, int matricula){
        super(nombre,apellidos,edad);
        this.matricula=matricula;

    }


    public void setMatricula(int matricula){
        this.matricula=matricula;
    }

     public int getMatricula(){
        return matricula;
    }
}

Everything compiles well, I only have those errors that do not show me what I need, in advance, thank you.

    
asked by G. Tary 14.02.2018 в 21:56
source

1 answer

2

You must overwrite the obtenerInformacion() method in the Maestro and Alumno classes.

In the Master class:

@Override    
public String obtenerInformacion() {
   return this.getNombre() + " " + this.getApellidos() + " " + this.getEdad() + " " + this.getClaveEmpleado();
}

And in the student class:

@Override
public String obtenerInformacion() {
   return this.getNombre() + " " + this.getApellidos() + " " + this.getEdad() + " " + this.getMatricula();
}

Greetings

    
answered by 14.02.2018 в 23:30