Preview

03 - Inheritance (Super and Sub classes)

 1. In Java all classes can inherit attributes (instance variables) and behaviors (methods) from another class.

  FALSE

  TRUE

 2. The class being inherited from is called the _______________. The class that is inheriting is called the child class or subclass.

  default class

  extended class

  parent class or superclass

  inheriting class/inherited class

 3. A car is a kind of ___________. This is sometimes called the is-a relationship, but more accurately it's a is-a kind of relationship.

  driver

  variable type

  vehicle

  wheel

 4. A _________ class diagram shows classes and the relationships between the classes
java_inheritiance_pic1.png

  OIL (Object instance language)

  OOP (Object Orientated Progress)

  UML (Unified Modeling Language)

  JAP (Java Adaptive Process)

 5. Refer to the above image. The parent class for Professor and Student is ________. The _______ class has ____ child classes or subclasses.

  Person / three

  Person / several

  Person / two

  Student / three

 6. Fill in the blanks for the following:
While a person can have two parents, 
a Java class can only inherit from one parent class. 

If you leave off the ________ keyword when you 
declare a class then the class will inherit 
from the Object class that is already defined
 in Java.

 7. The class Vehicle has the instance fields make and model. The class Car inherits from the class Vehicle but the car object will not automatically have the fields make and model.

  TRUE

  FALSE

 8. What type of inheritance does Java have?

  class zero inheritance

  single inheritance

  double inheritance

  karnaugh inheritance

 9. Say that there are three classes: Computer, AppleComputer, and IBMComputer. What are the likely relationships between these classes?

  IBMComputer is the superclass, AppleComputer and Computer are subclasses of IBMComputer.

  Computer is a superclass,AppleComputer is a subclasses of Computer, and IBMComputer is a sublclas of AppleComputer

  Computer,AppleComputer and IBMComputer are sibling classes.

  Computer is the superclass, AppleComputer and IBMComputer are subclasses of Computer.

 10. How many objects of a given class can there be in a program?

  One per defined class

  As many as the program needs

  One per main() method

  One per constructor definition.

 11. What restriction is there on using the super reference in a constructor?

  It must be used in the last statement of the constructor

  It can only be used in the parent's constructor

  It must be used in the first statement of the constructor

  Only one child class can use it

 12. Which of the following is correct syntax for defining a new class Cokeo based on the superclass SoftDrink?

  class Cokeo implements SoftDrink { //additional definitions go here }

  class Cokeo extends SoftDrink { //additional definitions go here }

  class Cokeo isa SoftDrink { //additional definitions go here }

  class Cokeo defines SoftDrink { //additional definitions go here }

 13. A class Animal has a subclass Mammal. Which of the following is true:

  Because of single inheritance, Mammal can have no siblings.

  Because of single inheritance, Mammal can have no other parent than Animal.

   Because of single inheritance, Mammal can have no subclasses.

  Because of single inheritance, Animal can have only one subclass.

 14. A subclass inherits both member variables and methods.

  TRUE

  FALSE

 15. Which of the following is NOT an advantage to using inheritance?

  One big superclass can be used instead of many little classes.

  Enhancements to a base class will automatically be applied to derived classes.

  Similar classes can be made to behave consistently.

  Code that is shared between classes needs to be written only once.

 16. A bookstore has an on-line ordering system. For each type of published material (books, movies, audio tapes) they need to track the id, title, author(s), date published, and price. What would be the best design?

   Create classes Book, Movie, and AudioTape with the requested fields.

  Create the class PublishedMaterial with children classes of Book, Movie, and AudioTape.

  Create one class PublishedMaterial with the requested fields plus type.

  Create one class BookStore with the requested fields plus type.

 17. Given the following class declarations, what is the output from Student s1 = new ComputingStudent(); followed by s1.getInfo();?
public class Student {
   public String getFood() {
      return "Macdonalds";
   }
   public String getInfo()  {
      return this.getFood();
   }
}

public class ComputingStudent extends Student {
   public String getFood() {
      return "Burger King";
   }
}

  Won't compile since you use this.getFood()

  Burger King

  Won't compile since GradStudent doesn't have a getInfo method

  Macdonalds

 18. Given the following class declarations and initializations in a client program, which of the following is a correct call to method1?
public class Test1
{
   public void method1(Test2 v1, Test3 v2)
   {
      // rest of method not shown
   }
}

public class Test2 extends Test1
{
}

public class Test3 extends Test2
{
}

The following initializations appear in a different class.
Test1 t1 = new Test1();
Test2 t2 = new Test2();
Test3 t3 = new Test3();

   t1.method1(t1,t1);

   t3.method1(t1,t1);

  t3.method1(t3,t3);

  2.method1(t2,t2);

 19. Assume that Base b = new Derived(); appears in a client program. What is the result of the call b.methodOne();?
public class Base
{
   public void methodOne()
   {
      System.out.print("A");
      methodTwo();
   }

   public void methodTwo()
   {
      System.out.print("B");
   }
}

public class Derived extends Base
{
   public void methodOne()
   {
      super.methodOne();
      System.out.print("C");
   }

   public void methodTwo()
   {
      super.methodTwo();
      System.out.print("D");
   }
}

  ABC

  AB

  ABDC

  ABCD

 20. Read the following excerpt and decide whether it is true or false.
If you have the following:

A parent class Animal that has a 
method speak() which returns: Awk. 

Cat has a speak method that returns: 
Meow. 

Bird does not have a speak method. 

Dog has a speak method that returns: Woof. 

Pig does not have a speak method. 

Cow has a speak method that returns: Moo. 

The output from looping through the array
'a' created below and asking each element to 
speak() would be:

Animal[] a = { new Cat(), new Cow(), new Dog(), 
new Pig(), new Bird() }

>>Meow Moo Woof Awk Awk

  FALSE

  TRUE