Answer:
The program to this question can be given as:
Program:
class Horse //define class Horse
{
private String name; //define variables.
private String color;
private int birthYear;
//define set method.
public void setName(String name)
{
this.name = name; //set value of name.
}
public void setColor(String color)
{
this.color = color; //set value of color.
}
public void setBirthYear(int birthYear)
{
this.birthYear = birthYear; //set value of birthYear
}
//define get method.
public String getName()
{
return name; //return name.
}
public String getColor()
{
return color; //return color.
}
public int getBirthYear()
{
return birthYear; //return birthYear.
}
}
class RaceHorse extends Horse //define class RaceHorse
{
private String raceNum; //define variable.
public void setRace(String raceNum) //define set method.
{
this.raceNum=raceNum; //set value of raceNum.
}
public String getRace()
{
return raceNum; //return raceNum
}
}
public class Main //define main class.
{
public static void main(String[] args) //define main method.
{
Horse ob= new Horse(); //creating class objects.
RaceHorse ob1 = new RaceHorse();
ob.setName("XXX"); //calling function
ob.setColor("BLACK");
ob.setBirthYear(2008);
ob1.setRace("Five hundred meter");
//print return values.
System.out.println("The name of the horse is " + ob.getName()+"." +"\n"+
"The horse's color is " + ob.getColor()+"."+"\n"+ "It was born in " + ob.getBirthYear() +
"."+"\n"+ ob.getName()+" has taken part in " + ob1.getRace() + " races.");
}
}
Output:
The name of the horse is XXX.
The horse's color is BLACK.
It was born in 2008.
XXX has taken part in Five hundred meter races.
Explanation:
In the above program firstly we declare the class that is Horse in this class we define a variable that name is already given in the question which are name, color, and birthYear. In these variables are name and color datatype is string and the birthYear datatype is an integer because it stores the number. Then we use the get and set function in the get function we return all variable value. and in the set function, we set the values of the variable by this keyword. Then we declare another class that is RaceHorse this class inherit Horse class. In the RaceHorse class, we define a variable that is raceNum that's data type is a string in this class we also used the get and set function. Then we declare the main class in the main class we define the main method in the main method we create the above class object and call the set and get function and print the values of the function.