Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. public class DemoHorses { public static void main(String args[]) { Horse horse1 = new Horse(); RaceHorse horse2 = new RaceHorse(); horse1.setName("Old Paint"); horse1.setColor("brown"); horse1.setBirthYear(2009); horse2.setName("Champion"); horse2.setColor("black"); horse2.setBirthYear(2011); horse2.setRaces(4); System.out.println(horse1.getName() + " is " + horse1.getColor() + " and was born in " + horse1.getBirthYear() + "."); System.out.println(horse2.getName() + " is " + horse2.getColor() + " and was born in " + horse2.getBirthYear() + "."); System.out.println(horse2.getName() + " has been in " + horse2.getRaces() + " races."); } }
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. public class DemoHorses { public static void main(String args[]) { Horse horse1 = new Horse(); RaceHorse horse2 = new RaceHorse(); horse1.setName("Old Paint"); horse1.setColor("brown"); horse1.setBirthYear(2009); horse2.setName("Champion"); horse2.setColor("black"); horse2.setBirthYear(2011); horse2.setRaces(4); System.out.println(horse1.getName() + " is " + horse1.getColor() + " and was born in " + horse1.getBirthYear() + "."); System.out.println(horse2.getName() + " is " + horse2.getColor() + " and was born in " + horse2.getBirthYear() + "."); System.out.println(horse2.getName() + " has been in " + horse2.getRaces() + " races."); } }
Chapter10: Introduction To Inheritance
Section: Chapter Questions
Problem 1GZ
Related questions
Question
Create a class named Horse that contains the following data fields:
- name - of type String
- color - of type String
- birthYear - of type int
Include get and set methods for these fields.
Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field.
public class DemoHorses
{
public static void main(String args[])
{
Horse horse1 = new Horse();
RaceHorse horse2 = new RaceHorse();
horse1.setName("Old Paint");
horse1.setColor("brown");
horse1.setBirthYear(2009);
horse2.setName("Champion");
horse2.setColor("black");
horse2.setBirthYear(2011);
horse2.setRaces(4);
System.out.println(horse1.getName() + " is " +
horse1.getColor() + " and was born in " + horse1.getBirthYear() + ".");
System.out.println(horse2.getName() + " is " +
horse2.getColor() + " and was born in " + horse2.getBirthYear() + ".");
System.out.println(horse2.getName() + " has been in " +
horse2.getRaces() + " races.");
}
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage