How can I call the show methodData of the child class in my main? Bearing in mind that there are builders

1
 public static void main(String[] args) {
    Persona[] persona = new Persona[5];
    persona[0] = new Estudiante("Java Avanzado", "Agustin", "Ozuna", 5445247, EstadoCivil.soltero);
    persona[1] = new Estudiante("Visual Basic", "Gilberto", "Ramirez", 451245, EstadoCivil.soltero);
    persona[2] = new Personal(Seccion.decanato, "Orlando", "Romero", 124578, EstadoCivil.casado);
    persona[3] = new Profesor(Departamento.lenguajes, "Castellano", "Juan", "Cristaldo", 154864, EstadoCivil.viudo);
    persona[4] = new Empleado(2005, TipoDecano.FIUNA, "Brayan", "Caceres", 666644, EstadoCivil.comprometido);
   }

class father person:

public abstract class Persona {

String nombre;
String apellido;
int nroId;
EstadoCivil estadoCivil;

public Persona(String nombre, String apellido, int nroId, EstadoCivil estadoCivil) {
    this.nombre = nombre;
    this.apellido = apellido;
    this.nroId = nroId;
    this.estadoCivil = estadoCivil;
}




public void mostrarDatos() {
    System.out.println("Metodo de la clase padre");

}

}

Student class that inherits person:

public class Estudiante extends Persona {

String curso;

public Estudiante(String curso, String nombre, String apellido, int nroId, EstadoCivil estadoCivil) {
    super(nombre, apellido, nroId, estadoCivil);
    this.curso = curso;
}


public String getCurso() {
    return curso;
}

public void setCurso(String curso) {
    this.curso = curso;
}

@Override
public void mostrarDatos() {
    System.out.println("\nNombre: " + nombre + "\nApellido: " + apellido + "\nCedula nro: " + nroId + "\nEstado Civil: " + estadoCivil  
            + "\nCurso: " + curso);

}

}

How can I call the show method of the Student class? And tried but I can not find how to call the method ..

    
asked by Agustin Ozuna Leguizamón 07.07.2018 в 02:47
source

1 answer

1

Your approach is fine as you have it, assuming of course that each daughter class has its method mostrarDatos() marked with @Override , you can read each element of the array like this:

    for(Persona p : persona){
        p.mostrarDatos();
    }

If you just want to show the Estudiantes data then you can put a condition inside the loop:

    for(Persona p : persona){
        if (p instanceof Estudiante) {
              p.mostrarDatos();
        }
     }

In that case, you will only invoke the mostrarDatos method when the p of the loop is an instance of Estudiante .

We can see it in an example in which an array has been created with three objects, two of the class Estudiante and one of the class Profesor .

The output, invoking the mostrarDatos method for each element of the array, would be like this:

Nombre: Agustin
Apellido: Ozuna
Cedula nro: 5445247
Curso: Java Avanzado

Nombre: Gilberto
Apellido: Ramirez
Cedula nro: 451245
Curso: Visual Basic

Nombre: Pedro
Apellido: Pérez
Cedula nro: 755555
Asignatura: Java y Android

DEMO

You can see here a DEMO IN REXTESTER

    
answered by 07.07.2018 / 03:43
source