Unit 9: Inheritance Flashcards
- Consider this inheritance hierarchy, in which Novel and Textbook are subclasses of Book.
Book / \ Novel Textbook
Which of the following is a false statement about the classes shown?
(A) The textbook class can have private instance variables that are neither in Book nor Novel.
(B) Each of the classes - Book, Novel, and Textbook - can have a method computeShelfLife, whose code in Book and Novel is identical, but different from the code in Textbook.
(C) If the Book class has private instance variables myTitle and myAuthor, then Novel and Textbook inherit them but cannot directly access them.
(D) Both Novel and Textbook inherit the constructors in Book.
(E) If the Book class has a private method called readFile, this method may not be accessed in either the Textbook or Novel class.
(D) Both Novel and Textbook inherit the constructors in Book.
Questions 2–17 refer to the Point, Quadrilateral, and Rectangle classes below: public class Point { private int xCoord; private int yCoord;
//constructor public Point(int x, int y) { ... }
//accessors public int get_x() { ... }
public int get_y() { ... } //Other methods are not shown. }
public abstract class Quadrilateral { private String labels; //e.g., "ABCD"
//constructor public Quadrilateral(String quadLabels) { labels = quadLabels; }
public String getLabels()
{ return labels; }
public abstract int perimeter();
public abstract int area();
}
public class Rectangle extends Quadrilateral { private Point topLeft; //coords of top left corner private Point botRight; //coords of bottom right corner
//constructor public Rectangle(String theLabels, Point theTopLeft, Point theBotRight) { /* implementation code */ }
public int perimeter()
{ /* implementation not shown */ }
public int area()
{ /* implementation not shown */ }
//Other methods are not shown. } 2. Which statement about the Quadrilateral class is false? (A) The perimeter and area methods are abstract because there’s no suitable default code for them.
(B) The getLabels method is not abstract because any subclasses of
Quadrilateral will have the same code for this method.
(C) If the Quadrilateral class is used in a program, it must be used as a super- class for at least one other class.
(D) No instances of a Quadrilateral object can be created in a program.
(E) Any subclasses of the Quadrilateral class must provide implementation code for the perimeter and area methods.
(E) Any subclasses of the Quadrilateral class must provide implementation code for the perimeter and area methods.
3. Which represents correct /* implementation code */ for the Rectangle construc- tor? I super(theLabels); II super(theLabels, theTopLeft, theBotRight); III super(theLabels); topLeft = theTopLeft; botRight = theBotRight; (A) I only (B) II only (C) III only (D) I and II only (E) II and III only
(C) III only
17. Refer to the Parallelogram and Square classes below. public class Parallelogram extends Quadrilateral { //Private instance variables and constructor are not shown. ... public int perimeter() { /* implementation not shown */ }
public int area()
{ /* implementation not shown */ }
}
public class Square extends Rectangle { //Private instance variables and constructor are not shown. ... public int perimeter() { /* implementation not shown */ }
public int area()
{ /* implementation not shown */ }
}
Consider an ArrayList quadList whose elements are of type
Rectangle, Parallelogram, or Square.
Refer to the following method, writeAreas:
/** Precondition: quadList contains Rectangle, Parallelogram, or
* Square objects in an unspecified order.
* @param quadList the list of quadrilaterals
*/
public static void writeAreas(List quadList)
{
for (Quadrilateral quad : quadList)
System.out.println(“Area of “ + quad.getLabels()
+ “ is “ + quad.area());
}
What is the effect of executing this method?
(A) The area of each Quadrilateral in quadList will be printed.
(B) A compile-time error will occur, stating that there is no area method in abstract class Quadrilateral.
(C) A compile-time error will occur, stating that there is no getLabels method
in classes Rectangle, Parallelogram, or Square.
(D) A NullPointerException will be thrown.
(E) A ClassCastException will be thrown.
(A) The area of each Quadrilateral in quadList will be printed.
5. Refer to the definitions of ClassOne and ClassTwo below. public class ClassOne { public void methodOne() { ... } //Other methods are not shown. }
public class ClassTwo extends ClassOne { public void methodTwo() { ... } //Other methods are not shown. }
Consider the following declarations in a client class. You may assume that ClassOne and ClassTwo have default constructors. ClassOne c1 = new ClassOne(); ClassOne c2 = new ClassTwo(); Which of the following method calls will cause an error? I c1.methodTwo(); II c2.methodTwo(); III c2.methodOne(); (A) None (B) I only (C) II only (D) III only (E) I and II only
(E) I and II only
6. Consider the following two classes. public class Bird { public void act() { System.out.print("fly "); makeNoise(); }
public void makeNoise() { System.out.print("chirp "); } }
public class Dove extends Bird { public void act() { super.act(); System.out.print("waddle "); }
public void makeNoise() { super.makeNoise(); System.out.print("coo "); } } Suppose the following declaration appears in a class other than Bird or Dove: Bird pigeon = new Dove(); What is printed as a result of the call pigeon.act()? (A) fly (B) fly chirp (C) fly chirp waddle (D) fly chirp waddle coo (E) fly chirp coo waddle
(E) fly chirp coo waddle