Chapter 8 Class Inheritance and Interfaces

 

1.         The printout is

The default constructor of A is invoked

 

2.         The default constructor of B attempts to invoke the default of constructor of A, but class A's default constructor is not defined.

 

3.         Find these terms in this chapter.

 

4.         The following lines are erroneous:

 

{ 

  radius = radius; // Must use this.radius = radius

}

 

class Cylinder extends Circle (missing extends)

 

{

  Circle(radius);  // Must use super(radius)

  length = length; // Must use this.length = length

}

 

public double findArea()

{

  return findArea()*length; // super.findArea()

}

 

 

 

{ 

  radius = radius; // Must use this.radius = radius

}

 

class Cylinder extends Circle (missing extends)

 

{

  Circle(radius);  // Must use super(radius)

  length = length; // Must use this.length = length

}

 

public double findArea()

{

  return findArea()*length; // super.findArea()

}

 

 

 

            5.         Indicate true or false for the following statements:

                        1.         True.

 

2.         False. (But yes in a subclass that extends the class where the protected datum is defined.)

 

3.         True.

 

4.         A final class can have instances.

                                    Answer: True

 

                        5.         An abstract class can have instances created using the constructor of the abstract class.

                                    Answer: No, but an instance of its concrete subclass is also an instance of the parent abstract class.

 

                        6.         A final class can be extended.

                                    Answer: False

 

                        7.         An abstract class can be extended.

                                    Answer: True

 

                        8.         A final method can be overridden.

                                    Answer: False

 

                        9.         You can always successfully cast a subclass to a superclass.

                                    Answer: True

 

                        10.       You can always successfully cast a superclass to a subclass.

                                    Answer: False

 

11.       An interface can be a separate unit and can be compiled into a bytecode file.

Answer: True

 

12.       The order in which modifiers appear before a method is important.

                                    Answer: False

 

13.            A subclass of a non-abstract superclass cannot be abstract. 

            Answer: False

 

14.            A subclass cannot override a concrete method in a superclass to declare it abstract.

Answer: False, This is rare, but useful when the implementation of the method in the superclass becomes invalid in the subclass. In this case, the subclass must be declared abstract.

 

6.         Method overloading defines methods of the same name in a class. Method overriding modifies the methods that are defined in the superclasses.

 

7.         Yes, because these two methods are defined in the Object class; therefore, they are available to all Java classes. The subclasses usually override these methods to provide specific information for these methods.

 

The toString() method returns a string representation of the object; the equals() method compares the contents of two objects to determine whether they are the same.

 

            8.         default visibility modifier.

 

9.         protected.

 

            10.       c.

 

11.       (circle instanceof Cylinder)

                        Answer: False

 

(cylinder instanceof Circle)

                        Answer: True

 

            12.       Yes, because you can always cast from subclass to superclass.

 

            13.       Yes.

 

     14.

          Is fruit instanceof Orange true?   false

          Is fruit instanceof Apple true?    true

          Is fruit instanceof GoldDelicious true? true

          Is fruit instanceof Macintosh true?     false

          Is orange instanceof Orange true?  true

          Is orange instanceof Fruit true?   true

          Is orange instanceof Apple true?   false

          Suppose the method makeApple is defined in the Apple class. Can fruit invoke this method? Yes

          Can orange invoke this method? No

          Suppose the method makeOrangeJuice is defined in the Orange class. Can orange invoke this method? Yes.

          Can fruit invoke this method? No.

 

            15.       d.

            16.

 

17.  a.  Test is a subclass of A. A does not have a default constructor. Test does not have a default constructor unless it is explicitly defined.

18.        B’s constructor is invoked

     A’s constructor is invoked

19.        d.

20.        Yes.

21.        No. The scope of the inner class is within its defining class.

22.        Yes.