Comp 284 - Homework 5 Solution

Based on Schildt, Chapter 7


  1. Defne the terms superclass and subclass.

    A superclass is a class that is inherited by a subclass.

  2. What Java keyword is used to inherit a class?

    extends

  3. A subclass inherits only the public members of its superclass. True or false?

    False. A subclass inherits all of the members of its superclass, but it cannot access the private members.

  4. When a subclass object is created, the constructor for its superclass is also called. True or false?

    True

  5. What form of super do you use to call a superclass constructor?

    super()

  6. What form of super is used to access a member of a superclass?

    super.member

  7. In a class hierarchy, in what order are the constructors called?

    Constructors are called in the order of derivation.

  8. A superclass reference can refer to a subclass object. True or false?

    True

  9. What is method overriding?

    When a method in a subclass has the same name and type signature as a method in a superclass, then the method in the subclass overrides the method in the superclass.

  10. How do overridden methods and superclass references support run-time polymorphism?

    Run-time polymorphism is achieved in Java through the mechanism of dynamic method dispatch. This is the process by which a call to an overridden method is resolved at run-time rather than at compile-time. When an overridden method is called through a superclass reference, the version of that method that is executed is the one defined by th object being referred to. Thus, calling the same method through the same superclass reference, but on different objects, results in different versions of the method being executed.

  11. What is an abstract method? What is an abstract class?

    An abstract method is a method modified by the abstract keyword. Abstract methods have no body. An abstract class is one that contains at least one abstract method. Abstract classes must also be qualified by the abstract keyword.

  12. When a subclass inherits an abstract class, what must the subclass do?

    A subclass must implement all abstract methods defined by its superclass, or declare itself as abstract.

  13. How do you prevent a class from being extended?

    To prevent a class from being extended, modify its declaration with final.

  14. How do you create a named constant in Java?

    Modify a class variable (static member) with final.

  15. What is Object?

    Object is the built-in class that is an implicit superclass for all other classes.