• Home
  • Textbooks
  • Starting Out with Java: From Control Structures through Objects
  • Inheritance

Starting Out with Java: From Control Structures through Objects

Tony Gaddis

Chapter 10

Inheritance - all with Video Answers

Educators


Chapter Questions

01:12

Problem 1

Here is the first line of a class declaration. What is the name of the supcrelass? What is the name of the subclass?
public class Truck extends vehicle

Aditya Sood
Aditya Sood
Numerade Educator
14:04

Problem 2

Look at the following class declarations and answer the questions that follow them:
public claaa shape
{
private double area public vold set\Lambdarea(double a)
}
area $=a$
\[
{
\]
public double gethrea()
}
return area:
\[
{
\]
}
public class CLrcle extends shape
{
private double radius? pub1Le vold setRadius (double $x$ )
}
radius $=x_{i}$ set\Lambdarea(Math.PI : $x$ " x)
{
public double getRadius()
}
retura radius?
{
}
a) Which class is the superclass? Which class is the subclass?
b) Draw a UML diagram showing the relationship between these two classes.
c) When a clrcle object is created, what are its public members?
d) What members of the shape class are not accessible to the clrcle class's methods?
c) Assume a program has the following declarations:
shape $s=$ new shape () i circle $c=$ new $c$ ircle (1) ;
Indicate whether the following statements are legal or illegal:
c. set Radius (10.0)
8. set Radius (10.0) Syatem, out .printin(c.gethrea())

Brian Ketelobeter
Brian Ketelobeter
Numerade Educator
00:57

Problem 3

Class $B$ extends class $A$. (Class $A$ is the superclass and class $B$ is the subclass.) Describe the order in which the class's constructors exccute when a class B object is created.

Aditya Sood
Aditya Sood
Numerade Educator
03:07

Problem 4

Look at the following classes:
public clasa Ground
{
public Ground()
\[
}
\]
Systen, out .printing "You are on the ground,"
\[
{
\]
}
public class sky extends Ground
{
public sky()
\[
}
\]
Syatem.out.printla("You are in the sky."):
\[
{
\]
}
What will the following program display?
public class Checkpoint
{
public static void main(stringl 1 arga) }
sky object - new sky():
\[
{
\]
}

SS
Sarvesh Somasundaram
Numerade Educator
01:39

Problem 5

Look at the following classes:
public class Ground
{
public Ground() 1
System. Out $.$ printla $($ "You are on the ground." )
}
publ Le Ground ( String groundColor)
\[
{
\]
Syatem. Out $.$ print 1 a $(\text { "The ground } 1 \text { s }=+$ groundcolor
\[
}
\]
{
public class sky extends Ground
}
public sky()
{
Syatem, out $\cdot$ print $1 n(\text { "You are } 1 \text { n the aky }, "$ )
}
public sky(string akycolor)
{
super $\left(" \operatorname{green}^{-0}\right)$
}
{
What will the following program display?
public claaa Checkpolat
}
public atatic void main(stringl 1 arga)
{
sky object = new sky("blue")
}

Manik Pulyani
Manik Pulyani
Numerade Educator
01:20

Problem 6

Under what circumstances would a subclass need to override a superclass method?

Pawan Yadav
Pawan Yadav
Numerade Educator
01:19

Problem 7

How can a subclass method call an overridden superclass method?

Manik Pulyani
Manik Pulyani
Numerade Educator
01:43

Problem 8

If a method in a subclass has the same signature as a method in the superclass, does the subclass method overload or override the superclass method?

Aditya Sood
Aditya Sood
Numerade Educator
01:43

Problem 9

If a method in a subclass has the same name as a method in the superclass, but uses a different parameter list, does the subclass method overload or override the superclass method?

Aditya Sood
Aditya Sood
Numerade Educator
01:19

Problem 10

How do you prevent a method from being overridden?

Manik Pulyani
Manik Pulyani
Numerade Educator
01:38

Problem 11

When a class member is declared as protected, what code may access it?

Aditya Sood
Aditya Sood
Numerade Educator
01:12

Problem 12

What is the difference between private members and protected members?

Jennifer Stoner
Jennifer Stoner
Numerade Educator
01:27

Problem 13

Why should you avoid making class members protected when possible?

SS
Sarvesh Somasundaram
Numerade Educator
01:12

Problem 14

What is the difference between private access and package access?

Jennifer Stoner
Jennifer Stoner
Numerade Educator
02:01

Problem 15

Why is it casy to give package access to a class member by accident?

SS
Sarvesh Somasundaram
Numerade Educator
00:57

Problem 16

Look at the following class definition:
public claaa Clasab extenda ClasaB
{
(Member Declarations $\dots .)$
}
Because clasab inherits from classb, is it true that classb does not inherit from the Object class? Why or why not?

Aditya Sood
Aditya Sood
Numerade Educator
01:10

Problem 17

When you create a class, it automatically has a tostring method and an equals method. Why?

Julie Silva
Julie Silva
Numerade Educator
01:47

Problem 18

Recall the Rectangle and cube classes discussed carlicr, as shown in Figure $10-18$
a) Is the following starement legal or illegal? If is is illegal, why?
Rectangle $x=$ new Cube (10,12,5)
b) If you determined that the statement in part a is legal, are the following statements legal or illcgal? (Indicate legal or illcgal for cach statement.) Syatem . OUt . Println ( $x$. gettength () ) System.out $.$ print 1 n $(x, \text { getwidth }())$ System . OUt . print $1 n(x . \text { get } \text { Helght }())$ Syatem.out $\cdot$ print $1 n(x \cdot \text { getsurtacentea }())$
c) Is the following statement legal or illegal? If it is illegal, why?
Cube $e$ - new Rectangle (10,12) ?

Vysakh M
Vysakh M
Numerade Educator
01:29

Problem 19

What is the purpose of an abstract method?

Aditya Sood
Aditya Sood
Numerade Educator
00:57

Problem 20

If a subclass extends a superclass with an abstract method, what must you do in the subclass?

Aditya Sood
Aditya Sood
Numerade Educator
01:29

Problem 21

What is the purpose of an abstract class?

Aditya Sood
Aditya Sood
Numerade Educator
01:29

Problem 22

If a class is defined as abstract, what can you not do with the class?

Aditya Sood
Aditya Sood
Numerade Educator
01:54

Problem 23

What is the purpose of an interface?

Jennifer Stoner
Jennifer Stoner
Numerade Educator
01:29

Problem 24

How is an interface similar to an abstract class?

Aditya Sood
Aditya Sood
Numerade Educator
01:29

Problem 24

How is an interface similar to an abstract class?

Aditya Sood
Aditya Sood
Numerade Educator
01:29

Problem 25

How is an interface different from an abstract class, or any class?

Aditya Sood
Aditya Sood
Numerade Educator
01:54

Problem 26

If an interface has ficlds, how are they treated?

Jennifer Stoner
Jennifer Stoner
Numerade Educator
00:51

Problem 27

Write the first line of a class named cuatomer, which implements an interface named Relatable.

Manik Pulyani
Manik Pulyani
Numerade Educator
03:05

Problem 28

Write the first line of a class named zmployee, which implements interfaces named Payable and Liatable.

SS
Sarvesh Somasundaram
Numerade Educator