Multiple Choice
1)
Inheritance is the process by which a new class – known as a _________ - is created from another class, called the _____________. base class, derived class derived class, base class inherited class, base class base class, inherited class Answer: B 2)
Inheritance promotes code ___________. reinvention reuse repeats
all of the above Answer: B 3)
The keyword extends indicates: encapsulation polymorphism inheritance
none of the above Answer: C 4)
A derived class is also called a sub class super class base class
all of the above Answer: A
Copyright © 2006 Pearson Education Addison-Wesley. All rights reserved. 1
2 Walter Savitch • Absolute Java 2/e Chapter 7 Test Bank
5)
A super class is also called a derived class dominant class sub class base class Answer: D
6)
What does a derived class automatically inherit from the base class? instance variables static variables public methods all of the above Answer: D
7)
If the final modifier is added to the definition of a method, this means: The method may be redefined in the derived class. The method may be redefined in the sub class.
The method may not be redefined in the derived class. None of the above. Answer: C
8)
A base class is synonymous with a: Child class Parent class Derived class Sub class Answer: B
9)
The special syntax for invoking a constructor of the base class is: super() base() parent() child() Answer: A
10) An object of a derived class has the type of the derived class, and it also has the type of the base
class, and more generally, has the type of every one of its ___________ classes. descendant child ancestor sub
Answer: C
Copyright © 2006 Pearson Education Addison-Wesley. All rights reserved.
Chapter 7 Inheritance 3
11) In using the keyword this in place of super(), the invocation of this must be the ___________
action taken by the constructor. first last
it does not matter none of the above
Answer: A
12) A method or instance variable modified by protected:
can not be accessed by name inside its own class definitions. can not be accessed by name inside any class derived from it.
can not be accessed by name in the definition of any class in the same package.
can not be accessed by name in any other class (that is, other than classes named in a-c.).
Answer: D
13) If an instance variable is not modified by public, protected or private then it is said to have:
Package access Default access Friendly access All of the above
Answer: D
14) The class __________ is an ancestor class of all Java classes.
String Object Math JFrame
Answer: B
15) The Object class contains the method:
getClass() toString() equals()
all of the above
Answer: D
16) The equals method for a class should have _________ as the type of its one parameter.
String Object Integer Double
Answer: B
Copyright © 2004 Addison-Wesley. All rights reserved.
4 Walter Savitch • Absolute Java 2/e Chapter 7 Test Bank
True/False
1)
A derived class contains only public instance variables and public methods from the base class. Answer: False 2)
Inheritance refers to a very specialized form of a class. Answer: False 3)
A derived class is a class defined by adding instance variables and methods to an existing class. Answer: True 4)
When you define a derived class, you give only the added instance variables and the added methods as well as all the methods from the base class. Answer: False 5)
The keyword extends indicates polymorphism. Answer: False 6)
Overriding is when a derived class redefines a method from the base class. Answer: True 7)
A constructor for a derived class begins with an invocation of a constructor for the base class. Answer: True 8)
The call to the base class constructor (super) must always be the last action taken in a constructor definition. Answer: False 9)
You may substitute the keyword this for super() to call a constructor of the derived class. Answer: True
10) An instance variable (or method) that is private in a base class is accessible by name in the definition
of a method in any other class.
Answer: False
11) Private methods of the base class are not available for use by derived classes.
Answer: True
Copyright © 2006 Pearson Education Addison-Wesley. All rights reserved.
Chapter 7 Inheritance 5
Short Answer/Essay
1)
Define a base class to represent a Clock. Your class should have instance variables for hours, minutes and seconds. Answer:
public class Clock {
private int hour; private int minute; private int second; public Clock() { }
public Clock(int h, int m, int s) {
setHour(h); setMinute(m);
Copyright © 2004 Addison-Wesley. All rights reserved.
//initialize to default values hour = 0; minute = 0; second = 0;
6 Walter Savitch • Absolute Java 2/e Chapter 7 Test Bank
} /**
setSecond(s);
Valid integers for hour is in the range of 0 - 24
*/
public void setHour(int h) { } /**
Valid integers for minute is in the range 0 - 59 */
public void setMinute(int m) {
if((m >= 0) && (m <= 59)) if((h >= 0) && (h <= 24)) else
System.out.println(\"Fatal error: invalid hour\"); hour = h;
Copyright © 2006 Pearson Education Addison-Wesley. All rights reserved.
Chapter 7 Inheritance 7
/**
}
else
minute = m;
System.out.println(\"Fatal error: invalid minute\");
Valid integers for second is in the range 0 - 59 */
public void setSecond(int s) { }
public int getHour() { }
return hour;
if((s >= 0) && (s <= 59)) else
System.out.println(\"Fatal error: invalid second\"); second = s;
Copyright © 2004 Addison-Wesley. All rights reserved.
8 Walter Savitch • Absolute Java 2/e Chapter 7 Test Bank
public int getMinute() {
return minute; }
public int getSecond() { }
//Facilitator methods public String toString() { }
public boolean equals(Object o) {
if(o == null)
return false;
return \"The current time is: \" + hour + \":\" + minute + \":\" + second; return second;
else if(getClass() != o.getClass())
Copyright © 2006 Pearson Education Addison-Wesley. All rights reserved.
Chapter 7 Inheritance 9
}
} 2)
}
else {
return false;
Clock otherClock = (Clock) o;
return((hour == otherClock.hour) && (minute == otherClock.minute)
&& (second == otherClock.second));
Explain the difference between method overloading and method overriding.
Answer: Overriding refers to redefining a method definition in a derived class. When a method definition is overridden, the new method definition has the exact same number and types of
parameters. An overloaded method has the same name as another method in the class, but number and/or type of parameters differ.
Explain the modifiers public, protected and private.
Answer: The public modifier signifies that an instance variable or method is inherited freely by the derived class. The protected modifier signifies that an instance variable or method is available by name inside its own class definition, as well as by any class derived from it or in the same package. The private modifier signifies that an instance variable or method is not available outside of its class definition.
What does the instanceof operator do?
Answer: The instanceof operator checks to see if an object is of the type given as its second argument.
Create a class to represent a Rectangle. Your class should contain instance variables for length and width, as well as member method to calculate the area and perimeter. Answer:
public class Rectangle
3)
4)
5)
Copyright © 2004 Addison-Wesley. All rights reserved.
10 Walter Savitch • Absolute Java 2/e Chapter 7 Test Bank
{
private double length; private double width; public Rectangle() {
}
public Rectangle(double l, double w) {
}
public void setLength(double l) {
if(l >= 0) else
length = l;
setLength(l); setWidth(w);
length = 0; width = 0;
Copyright © 2006 Pearson Education Addison-Wesley. All rights reserved.
Chapter 7 Inheritance 11
}
System.out.println(\"Fatal error: Length may not be negative!\");
public void setWidth(double w) { }
public double getLength()
{ }
public double getWidth() { }
public double getArea()
return width; return length;
if(w >= 0) else
System.out.println(\"Fatal error: Width may not be negative!\"); width = w;
Copyright © 2004 Addison-Wesley. All rights reserved.
12 Walter Savitch • Absolute Java 2/e Chapter 7 Test Bank
{ }
public double getPerimeter() {
return length * width;
}
return 2 * (length + width);
public String toString() {
}
public boolean equals(Object o) {
if(o == null)
return false;
return \"Length = \" + length + \"\\nWidth = \" + width;
else if(getClass() != o.getClass()) else
return false;
Copyright © 2006 Pearson Education Addison-Wesley. All rights reserved.
Chapter 7 Inheritance 13
{
Rectangle otherRectangle = (Rectangle) o;
return((length == otherRectangle.length) &&
(width == otherRectangle.width));
}
} 6)
}
Create a test driver to test the functionality of your Rectangle class created in number 14 above. Answer:
public class RectangleTest {
public static void main(String args[]) {
Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle(5,6); Rectangle r3 = r2;
System.out.println(r1.toString() + \"\\n\" + r2.toString() +
\"\\n\" + r3.toString());
r1.setLength(8);
Copyright © 2004 Addison-Wesley. All rights reserved.
14 Walter Savitch • Absolute Java 2/e Chapter 7 Test Bank
}
}
r1.setWidth(10);
System.out.println(\"The area of r1 = \" + r1.getArea());
System.out.println(\"The perimeter of r1 = \" + r1.getPerimeter()); if(r1.equals(r2)) else
System.out.println(\"r1 and r2 are not equal!\"); System.out.println(\"r1 and r2 are equal!\");
if(r2.equals(r3)) else
System.out.println(\"r2 and r3 are not equal!\");
System.out.println(\"The area of r2 and r3 = \" + r2.getArea());
Copyright © 2006 Pearson Education Addison-Wesley. All rights reserved.
因篇幅问题不能全部显示,请点此查看更多更全内容