• Home
  • Textbooks
  • Java How To Program. Early Objects
  • Object-Oriented Programming: Inheritance

Java How To Program. Early Objects

Paul Deitel, Harvey Deitel

Chapter 9

Object-Oriented Programming: Inheritance - all with Video Answers

Educators


Chapter Questions

01:21

Problem 1

Fill in the blanks in each of the following statements:
a) __________ is a form of software reusability in which new classes acquire the members of existing classes and embellish those classes with new capabilities.
b) A superclass’s____________ members can be accessed in the superclass declaration and in subclass declarations.
c) In a(n) _______ relationship, an object of a subclass can also be treated as an object of its superclass.
d) In a(n) _______ relationship, a class object has references to objects of other classes as members.
e) In single inheritance, a class exists in a(n) _______________ relationship with its subclasses.
f) A superclass’s __________ members are accessible anywhere that the program has a reference to an object of that superclass or to an object of one of its subclasses.
g) When an object of a subclass is instantiated, a superclass ______________ is called implicitly or explicitly.]
h) Subclass constructors can call superclass constructors via the _______ keyword.

Manik Pulyani
Manik Pulyani
Numerade Educator
01:12

Problem 2

State whether each of the following is true or false. If a statement is false, explain why.
a) Superclass constructors are not inherited by subclasses.
b) $\mathrm{A}$ has-a relationship is implemented via inheritance.
c) $A$ Car class has an $i s-a$ relationship with the SteeringWhee 7 and Brakes classes.
d) When a subclass redefines a superclass method by using the same signature, the subclass is said to overload that superclass method.

Aditya Sood
Aditya Sood
Numerade Educator
01:27

Problem 3

$(\text {Using Composition Rather Than Inberitance})$ Many programs written with inheritance could be written with composition instead, and vice versa. Rewrite class BaseP 7 us CommissionEmployee (Fig. 9.11) of the CommissionEmployee-BasePlusCommissionEmployee hierarchy to use composition rather than inheritance.

SS
Sarvesh Somasundaram
Numerade Educator
01:28

Problem 4

$(\text {Software Reuse})$ Discuss the ways in which inheritance promotes software reuse, saves time during program development and helps prevent errors.

Jennifer Stoner
Jennifer Stoner
Numerade Educator
02:56

Problem 5

(Student Inberitance Hierarchy) Draw an inheritance hierarchy for students at a university similar to the hierarchy shown in Fig. 9.2. Use Student as the superclass of the hierarchy, then extend Student with classes UndergraduateStudent and GraduateStudent. Continue to extend the hierarchy as deep (i.e., as many levels) as possible. For example, Freshman, Sophomore, Junior and Senior might extend UndergraduateStudent, and Doctoral Student and MastersStudent might be subclasses of GraduateStudent. After drawing the hierarchy, discuss the relationships that exist between the classes. [Note: You do not need to write any code for this exercise.]

SS
Sarvesh Somasundaram
Numerade Educator
02:56

Problem 6

(Shape Inheritance Hierarchy) The world of shapes is much richer than the shapes included in the inheritance hierarchy of Fig. 9.3. Write down all the shapes you can think of - both two-dimensional and three-dimensional-and form them into a more complete Shape hierarchy with as many levels as possible. Your hierarchy should have class Shape at the top. Classes TwoDimensiona 1 Shape and ThreeDimensional Shape should extend Shape. Add additional subclasses, such as Quadri lateral and Sphere, at their correct locations in the hierarchy as necessary.

SS
Sarvesh Somasundaram
Numerade Educator
01:18

Problem 7

(protected vs. private) Some programmers prefer not to use protected access, because they believe it breaks the encapsulation of the superclass. Discuss the relative merits of using protected access vs. using private access in superclasses.

Jennifer Stoner
Jennifer Stoner
Numerade Educator
14:04

Problem 8

(Quadritateral Inberitance Hierarchy) Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep (i.e., as many levels) as possible. Specify the instance variables and methods for each class. The private instance variables of Quadrilateral should be the $x-y$ coordinate pairs for the four endpoints of the Quadrilateral. Write a program that instantiates objects of your classes and outputs each object's area (except Quadrilateral).

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator
01:55

Problem 9

(What Does Each Code Snippet Do?)
a) Assume that the following method call is located in an overridden earnings method in a subclass:
super .earnings()
b) Assume that the following line of code appears before a method declaration:
QOverride
c) Assume that the following line of code appears as the first statement in a constructor's body:
super(firstArgument, secondArgument);

James Kiss
James Kiss
Numerade Educator
02:52

Problem 10

Write a Line of Code) Write a line of code that performs each of the following tasks:
a) Specify that class Pieceworker inherits from class Employee.
b) Call superclass Employee's toString method from subclass Pieceworker's toString method.
c) Call superclass Employee's constructor from subclass Pieceworker's constructor-assume that the superclass constructor receives three Strings representing the first name, last name and social security number.

Jordan Gassaway
Jordan Gassaway
Numerade Educator
01:55

Problem 11

$(U \text { sing super in a Constructor's } B \text { od } y$ ) Explain why you would use super in the first statement of a subclass constructor's body.

James Kiss
James Kiss
Numerade Educator
01:55

Problem 12

(Using super in an Instance Method’s Body) Explain why you would use super in the body of a subclass’s instance method.

James Kiss
James Kiss
Numerade Educator
01:55

Problem 13

(Calling get Methods in a Class’s Body) In Figs. 9.10–9.11 methods earnings and to String each call various get methods within the same class. Explain the benefits of calling these get methods within the classes.

James Kiss
James Kiss
Numerade Educator
02:44

Problem 14

$(Employee Hierarchy) In this chapter, you studied an inheritance hierarchy in which class BaseP 7 us CommissionEmployee inherited from class CommissionEmployee. However, not all types of employees are CommissionEmployees. In this exercise, you'll create a more general Employee superclass. that factors out the attributes and behaviors in class CommissionEmployee that are common to all Employees. The common attributes and behaviors for all Employees are firstName, lastName, socialsecurityNumber, getFirstName, getLastName, getSocial SecurityNumber and a portion of method toString. Create a new superclass Employee that contains these instance variables and methods and a constructor. Next, rewrite class CommissionEmployee from Section 9.4 .5 as a subclass of Employee. Class CommissionEmployee should contain only the instance variables and methods that are not declared in superclass Employee. Class CommissionEmployee's constructor should invoke class Employee's constructor and CommissionEmployee's toString method should invoke Employee's toString method. Once you've completed these modifications, run the CommissionEmployeeTest and BasePlusCommissionEmployeeTest apps using these new classes to ensure that the apps still display the same results for a CommissionEmployee object and BaseP 7 usCommissionEmployee object, respectively.

Akash M
Akash M
Numerade Educator
02:44

Problem 15

(Creating a New Subclass of Employee) Other types of Employees might include SalariedEmployees who get paid a fixed weekly salary, Pieceworkers who get paid by the number of pieces they produce or Hour 7 yemployees who get paid an hourly wage with time-and-a-half- 1.5 times the hourly wage-for hours worked over 40 hours.
Create class HourTyEmployee that inherits from class Employee (Exercise 9.14) and has instance variable hours (a double) that represents the hours worked, instance variable wage (a double that represents the wages per hour, a constructor that takes as arguments a first name, a last name, a social security number, an hourly wage and the number of hours worked, set and get methods for manipulating the hours and wage, an earnings method to calculate an HourlyEmployee's earnings based on the hours worked and a toString method that returns the HourlyEmployee's String representation. Method setwage should ensure that wage is nonnegative, and setHours should ensure that the value of hours is between 0 and 168 (the total number of hours in a week). Use class Hour 7 yemployee in a test program that's similar to the one in Fig. 9.5.

Akash M
Akash M
Numerade Educator