Create a class named Horse that contains data fields for the
Create a class named Horse that contains data fields for the name, color, and birth year. Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field that holds the number of races in which the horse has competed and additional methods to get and set the new field. Write an application that demonstrates using objects of each class. Save the files as Horse.java, RaceHorse.java, and DemoHorses.java.
Solution
DemoHorses.java
import javax.swing.JOptionPane;
public class DemoHorses{
public class void main(String[ ] args){
Horse h = new Horse();
RaceHorse rh = new RaceHorse();
h.setName();
h.setColor();
h.setYear();
rh.SetRace();
JOptionPane.showMessageDialog(null,\"The name of the horse is \" +h.getName(); + \". THe horse\'s color \" + h.getColor()+\". It was born in \" +h.getYear()+\".\" + h.getName()+\" has taken part in \" +rh.getRace()+\" races\");
}
}
Horse.java
import javax.swing.JOptionPane;
public class Horse{
private String name;
private String color;
private String birthYear;
public void setName(){
name=JOptionPane.showInputDialog(null,\"Please enter name of the horse\");
}
public void setColor(){
color=JOptionPane.showInputDialog(null,\"Please enter color of the horse\");
}
public void setYear(){
birthYear-JoptionPane.showInputDialog(null,\"Please enter the birth year of thr Horse\");
}
public String getName(){
return name;
}
public String getcolor(){
return color;
}
public String getYear(){
return birthYear;
}
}
RaceHorse.java
import javax.swing.JOptionPane;
public class RaceHorse extends Horse{
private String raceNum;
public void setRace(){
raceNum=JOptionPane.showInputDialog(null,\"Please enter number of horse\'s races\");
}
public void getRace(){
return raceNum;
}
}

