Using toString() method from child class

Using toString() method from child class

Hi I am writing a code using polymorphism and I would like to print List on the screen but when I am using my code it run toString method from parent class only. How can I fix it?

Hi I am writing a code using polymorphism and I would like to print List on the screen but when I am using my code it run toString method from parent class only. How can I fix it?

public class HospitalApp {

public static void main(String[] main){

Hospital hospital = new Hospital();

List<Person> lista = new ArrayList<>();

lista = hospital.showList();


StringBuilder stringBuilder = new StringBuilder();

for(Person person : lista){
    stringBuilder.append(person);
    stringBuilder.append("\n");
}
System.out.println(stringBuilder.toString());

}

}


public class Hospital{

List<Person> lista = new ArrayList<>();
Person doktor = new Doctor("All", "Bundy",100, 99999);
Person nurse = new Nurse("Iga", "Lis",160, 10);
Person nurse_1 = new Nurse("Magda", "Andrych",160, 20);

public List showList(){ lista.add(doktor); lista.add(nurse); lista.add(nurse_1); return lista; }

}


public class Person{

private String imie;
private String nazwisko;
private double wyplata;

public Person(){}

public Person(String imie, String nazwisko, double wyplata){ this.imie = imie; this.nazwisko = nazwisko; this.wyplata = wyplata; }

public void setImie(String imie){ this.imie = imie; }

public String getImie(){ return imie; }

public void setNazwisko(String nazwisko){ this.nazwisko = nazwisko; }

public String getNazwisko(){ return nazwisko; }

public void setWyplata(double wyplata){ this.wyplata = wyplata; }

public double getWyplata(){ return wyplata; }

public String toString(){ return getImie() + " " + getNazwisko() + " " + getWyplata(); }

}


public class Nurse extends Person{

private int nadgodziny;

public Nurse(){} public Nurse(String imie, String nazwisko, double wyplata, int nadgodziny){ super(imie, nazwisko, wyplata); this.nadgodziny = nadgodziny; }

public void setNadgodziny(int nadgodziny){ this.nadgodziny = nadgodziny; }

public int getNadgodziny(){ return nadgodziny; }

@Override String toString(){ return getImie() + " " + getNazwisko() + " " + getWyplata() + " " + getNadgodziny(); }

}


public class Doctor extends Person {

private double premia;

public Doctor(){}

public Doctor(String imie, String nazwisko, double wyplata , double premia){ super(imie, nazwisko, wyplata); this.premia = premia; }

public double getPremia(){ return premia; }

public void setPremia(double premia){ this.premia = premia; }

@Override String toString(){ return getImie() + " " + getNazwisko() + " " + getWyplata() + " " + getPremia(); }

}

Can someone help me solve this problem?

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Java String – String Functions In Java With Examples

In this post, we will be discussing about a new concept, Java String. String is a sequence of characters. But in Java, a string is an object that represents a sequence of characters. The java.lang.String class is used to create string object.

String Initialization in Java

Learn about the difference between declaration and initialization of String in Java.

How to write @ as string in Java

How do I write @ as an string in java. When I write "@" it indicate as missing resource type This is my TextView