Which of the following keywords allows a subclass to call a superclass method even when the subclass has overridden the superclass method?

23 Questions | Total Attempts: 1545

  •  Which of the following statements is false? 

    • A subclass is generally larger than its superclass.

    • A superclass object is a subclass object.

    • The class following the extends keyword in a class declaration is the direct superclass of the class being declared.

    • Java uses interfaces to provide the benefits of multiple inheritance.

  •  Inheritance is also known as the

  •  Which of the following is not a superclass/subclass relationship?

    • University/Brown University

  • : An advantage of inheritance is that:

    • All methods can be inherited

    • All instance variables can be uniformly accessed by subclasses and superclasses.

    • Objects of a subclass can be treated like objects of their superclass.

  • Which of the following keywords allows a subclass to access a superclass method even when the subclass has overridden the superclass method?

  • Using the protected keyword gives a member:

  • Superclass methods with this level of access cannot be called from subclasses.

  •  Every class in Java, except ________, extends an existing class.

  • Overriding a method differs from overloading a method because:

    • A. Overloaded methods have the same signature.

    • B. Overridden methods have the same signature.

  • 9.4.2 Q1: To avoid duplicating code, use ________, rather than ________.

    • A. inheritance, the “copy-and-past” approach.

    • B. the “copy-and-past” approach, inheritance.

    • C. a class that explicitly extends Object, a class that does not extend Object.

    • D. a class that does not extend Object, a class that explicitly extends Object.

  • Consider the classes below, declared in the same file:class A {   int a;   public A()    {      a = 7;   }}class B extends A {   int b;   public B()    {b = 8;   }    }Which of the statements below is false?

    • A. Both variables a and b are instance variables.

    • B. After the constructor for class B executes, the variable a will have the value 7.

    • C. After the constructor for class B executes, the variable b will have the value 8.

    • D. A reference of type A can be treated as a reference of type B.

  • 9.4.3 Q2: Which of the following is the superclass constructor call syntax?

    • A. keyword super, followed by a dot (.) .

    • B. keyword super, followed by a set of parentheses containing the superclass constructor arguments.

    • C. keyword super, followed by a dot and the superclass constructor name.

  • Which superclass members are inherited by all subclasses of that superclass?

    • A. private instance variables and methods.

    • B. protected instance variables and methods.

    • D. protected constructors.

  • 9.4.4 Q2: Which statement is true when a superclass has protected instance variables?

    • A. A subclass object can assign an invalid value to the superclass’s instance variables, thus leaving an object in an inconsistent state.

    • B. Subclass methods are more likely to be written so that they depend on the superclass’s data implementation.

    • C. We may need to modify all the subclasses of the superclass if the superclass implementation changes.

  • 9.4.5 Q1: private fields of a superclass can be accessed in a subclass

    • A. by calling private methods declared in the superclass.

    • B. by calling public or protected methods declared in the superclass.

  • Failure to prefix the superclass method name with the keyword super and a dot (.) separator when referencing the superclass’s method causes ________.

  • 9.5 Q1: When a subclass constructor calls its superclass constructor, what happens if the superclass’s constructor does not assign a value to an instance variable?

    • A. A syntax error occurs.

    • B. A compile-time error occurs.

    • C. A run-time error occurs.

    • D. The program compiles and runs because the instance variables are initialized to their default values.

  • 9.6 Q1: Which of the following statements is (are) true?A.    We can use inheritance to customize existing software.B.    A superclass specifies commonality.C.    A superclass can be modified without modifying subclassesD.    A subclass can be modified without modifying its superclass.

  • 9.6 Q2: Which of the following is an example of a functionality that should not be “factored out” to a superclass?

    • A. Both ducks and geese are birds that know how to start flying from the water.

    • B. All vehicles know how to start and stop.

    • C. All animals lay eggs, except for mammals.

    • D. All paints have a color.

  • 9.7 Q1: The default implementation of method clone of Object performs a ________.

  • 9.7 Q2: The default equals implementation determines:

    • A. whether two references refer to the same object in memory.

    • B. whether two references have the same type.

    • C. whether two objects have the same instance variables.

    • D. whether two objects have the same instance variable values.

  • 9.8 Q1: Class ________ represents an image that can be displayed on a JLabel.

  • 9.8 Q2: Which method changes the text the label displays?

  • HTML
  • Server
  • PHP
  • Computer Science
  • SQL

If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super. You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass:

public class Superclass { public void printMethod() { System.out.println("Printed in Superclass."); } }

Here is a subclass, called Subclass, that overrides printMethod():

public class Subclass extends Superclass { // overrides printMethod in Superclass public void printMethod() { super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } }

Within Subclass, the simple name printMethod() refers to the one declared in Subclass, which overrides the one in Superclass. So, to refer to printMethod() inherited from Superclass, Subclass must use a qualified name, using super as shown. Compiling and executing Subclass prints the following:

Printed in Superclass. Printed in Subclass

Subclass Constructors

The following example illustrates how to use the super keyword to invoke a superclass's constructor. Recall from the Bicycle example that MountainBike is a subclass of Bicycle. Here is the MountainBike (subclass) constructor that calls the superclass constructor and then adds initialization code of its own:

public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; }

Invocation of a superclass constructor must be the first line in the subclass constructor.

The syntax for calling a superclass constructor is

or:

With super(), the superclass no-argument constructor is called. With super(parameter list), the superclass constructor with a matching parameter list is called.

Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Object. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent.