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.
public class Horse {
private String name;
private String color;
private int birthYear;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBirthYear() {
return birthYear;
}
public void setBirthYear(int birthYear) {
this.birthYear = birthYear;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class RaceHorse extends Horse{
private int numOfRaces;
public int getNumOfRaces() {
return numOfRaces;
}
public void setNumOfRaces(int numOfRaces) {
this.numOfRaces = numOfRaces;
}
}
public class DemoHorses {
public static void main(String[] args) {
Horse horse = new Horse();
horse.setName("Kaze");
horse.setBirthYear(2010);
horse.setColor("Brown");
RaceHorse raceHorse = new RaceHorse();
raceHorse.setName("Kage");
raceHorse.setBirthYear(2019);
raceHorse.setColor("White");
raceHorse.setNumOfRaces(10);
System.out.println(horse.getName()+" "+horse.getBirthYear()+" year, "+horse.getColor());
System.out.println(raceHorse.getName()+" "+raceHorse.getBirthYear()+" year, "
+raceHorse.getColor()+", races: "+raceHorse.getNumOfRaces());
}
}
Comments
Leave a comment