Chapter 10

¡Supera tus tareas y exámenes ahora con Quizwiz!

True or False: An object of a superclass can access members declared in a subclass.

False

True or False: Constructors are not inherited.

False

True or False: If a subclass constructor does not explicitly call a superclass construc-tor, Java will not call any of the superclass's constructors.

False

True or False: When a method is declared with the final modifier, it must be overrid-den in a subclass.

False

Write the first line of a class named Customer, which implements an interface named Relatable.

public class Customer implements Relatable

Write the first line of a class named Employee, which implements interfaces named Payable and Listable

public class Employee implements Payable, Listable

Sometimes you need a class that is simple, and to be instantiated only once in your code. When this is the case, you can use an _____________________

anonymous inner class.

In a subclass constructor, a call to the superclass constructor must __________.

appear as the very first statement

superclasses are also called__________________

base classes

This key word indicates that a class inherits from another class.

extends

An __________ class is a class that is defined inside another class. An anonymous inner class is an inner class that has_______. An anonymous inner class must_____________an interface, or _______another class.

inner, no name, implement , extend

In a UML diagram, an interface is drawn like a class, except the interface name and the method names are___________, and the <> tag is shown above the interface name.

italicized

Abstract classes are drawn like regular classes in UML, except the name of the class and the names of abstract methods are shown in ______

italics.

A functional interface is an interface that has one abstract method. You can use a special type of expression, known as a________________, to create an object that implements a functional interface.

lambda expression

If two methods have the same name but different signatures, they are ______________This is true where the methods are in the same class or where one method is in the super-class and the other method is in the subclass.

overloaded.

Abstract methods must be __________.

overridden

To use more than one parameter in a lambda expression, simply write a comma-separated list and enclose the list in______________. Here is an example: (a, b) -> a + b;

parentheses

A subclass does not have access to these superclass members.

private

These superclass members are accessible to subclasses and classes in the same package.

protected

When a method overrides another method, however, they both have the same _____________

signature.

The ___________ is the general class and the _________ is the specialized class.

superclass, subclass

In an inheritance relationship, this is the specialized class.

subclass

In Java, a reference variable is polymorphic because it can reference objects of types different from its own, as long as those types are ____________of its type.

subclasses

T/F A superclass reference variable can reference objects of a subclass.

True

The super statement that calls the superclass constructor must be the_______ statement in the subclass's constructor. This is because the superclass's constructor must execute before the code in the subclass's constructor executes.

first

An anonymous inner class must __________.

implement an interface or extend a superclass

An interface reference variable can reference any object that ________________ that interface, regardless of its class type.

implements

When you want a class to implement an interface, you use the _______________ key word in the class header.

implements

You can use a lambda expression to instantiate an object that __________.

implements a functional interface

An interface looks similar to a class, except the key word__________is used instead of the key word class, and the methods that are specified in an interface have no bodies, only headers that are terminated by semicolons

interface

You use the __________ operator to define an anonymous inner class.

new

_________________ is when a method has the same name as one or more other methods, but a different parameter list

overloading

When a class implements an interface, it must __________

provide all of the nondefault methods that are listed in the interface, with the exact signatures and return types specified

all methods in an interface are implicitly_________ You can optionally write public in the method header, but most programmers leave it out because all interface methods must be public.

public.

The following is an explicit call to the superclass's default constructor.

super();

In an inheritance relationship, this is the general class.

superclass

If an interface has fields, how are they treated?

final and static

A class becomes abstract when you place the_________ key word in the class definition. Here is the general format: AccessSpecifier abstract class ClassName An abstract class is not instantiated, but other classes extend it. An abstract method has no body and must be overridden in a subclass.

abstract

An_________ class is not instantiated, but other classes extend it. An abstract method has no body and must be overridden in a subclass.

abstract

Notice that the key word abstract appears in the header, and that the header ends with a semicolon. There is no body for the method.

abstract method

As was mentioned earlier, the GradedActivity class has only one constructor, which is the default constructor that Java automatically generated for it. When a FinalExam object is created, the GradedActivity class's default constructor is executed just _______ the FinalExam constructor is executed

before

An interface can contain field declarations, but all fields in an interface are treated as final and static. Because they automatically become final, you must provide an __________________

initialization value

This operator can be used to determine whether a reference variable references an object of a particular class.

instanceof

An ___________ specifies behavior for a class.

interface

. A method in a subclass having the same name as a method in the superclass but a dif-ferent signature is an example of __________.

overloading

If a method in a subclass has the same signature as a method in the superclass, does the subclass method overload or override the superclass method?

override

When a class implements an interface with a default method, the class can_________ the default method, but it is not required to.

override

If a subclass extends a superclass with an abstract method, what must you do in the subclass?

override the abstract method

A subclass may have a method with the same signature as a superclass method. In such a case, the subclass method_________the superclass method.

overrides

If a method in a subclass has the same signature as a method in the superclass, the subclass method___________ the superclass method.

overrides

A method in a subclass that has the same signature as a method in the superclass is an example of __________.

overriding

If you do not provide an access specifier for a class member, the class member is given ____________ access by default. This means that any method in the same package may access the member.

package

The ____ key word refers to an object's superclass. You can use the super key word to call a superclass constructor.

super

This key word refers to an object's superclass.

super

How can a subclass method call an overridden superclass method?

super.methodName()

When a class implements an interface, it is agreeing to provide all of the methods that are specified by the interface. It is often said that an interface is like a__________and when a class implements an interface it must adhere to the contract.

"contract,"

In object-oriented programming, inheritance is used to create an _______relationship among classes. This allows you to extend the capabilities of a class by creating another class that is a specialized version of it.

"is a"

If a functional interface's abstract method has no parameters, any lambda expression that you use with the interface must also have no parameters.

() -> System.out.println();

lambda operator

(->)

Remember the following points about abstract methods and classes:

Abstract methods and abstract classes are defined with the abstract key word. Abstract methods have no body, and their header must end with a semicolon. • An abstract method must be overridden in a subclass. • When a class contains an abstract method, it cannot be instantiated. It must serve as a superclass. • An abstract class cannot be instantiated. It must serve as a superclass.

How is an interface similar to an abstract class?

Can't be instantiated and all methods must be written elsewhere

If a class is defined as abstract, what can you not do with the class?

Can't instantiate abstract class

What is the purpose of an abstract class?

Cannot be instantiated, must serve as superclass

How do you prevent a method from being overridden?

Declare method with final modifier

True or False: A class may only implement one interface.

False

True or False: A subclass reference variable can reference an object of the superclass.

False

What is inheritance

Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends.

5 How is an interface different from an abstract class, or any class?

Interface only specifies methods, does not define them. All members of interface are public

All classes directly or indirectly inherit from this class.

Object

The Java API has a class named __________, which all other classes directly or indirectly inherit from.

Object

Because every class directly or indirectly extends the_______ class, every class inherits the _____ class's members. Two of the most useful are the ______ ________ methods.

Object, Object, toString and equals

____________ members of a class may be accessed by methods in a subclass, and by methods in the same package as the class

Protected

What is the purpose of an interface?

Specifies behavior for a class

Here is the first line of a class declaration. What is the name of the superclass? What is the name of the subclass? public class Truck extends Vehicle

Superclass Vehicle, Subclass Truck

x-> x * x

The x that appears on the left side of the -> operator is the name of a parameter variable, and the expression x * x that appears on the right side of the -> operator is the value that is returned

If a method in a subclass has the same name as a method in the superclass, but uses a different parameter list, does the subclass method overload or override the superclass method?

overload

Because an anonymous inner class's definition is written inside a method, it can access that method's local variables, but only if they are declared final, or if they are effec-tively final. (An effectively final variable is a variable whose value is never changed.) A ________________will result if an anonymous inner class tries to use a variable that is not final, or not effectively final.

compiler error

The super statement that calls the superclass constructor may be written only in the sub-class's ____________. You cannot call the superclass constructor from any other method.

constructor

subclasses are also called _______________

derived classes.

In the Object class, the _______ method returns a reference to a String containing the object's class name, followed by the @ sign, followed by the object's hash code, which is a hexadecimal number.

toString

The equals method accepts a reference to an object as its argument. It returns_______if the argument references the calling object.

true

T/F If a superclass does not have a default constructor and does not have a no-arg constructor, then a class that inherits from it must call one of the constructors that the superclass does have. If it does not, an error will result when the subclass is compiled.

True

What is the purpose of an abstract method?

To ensure that the subclass implements the method

T/F Although making a class member protected instead of private might make some tasks easier, you should avoid this practice when possible because any class that inherits from the class, or is in the same package, has unrestricted access to the protected member. It is always bet-ter to make all fields private and then provide public methods for accessing those fields.

True

T/F The instanceof operator understands the "is-a" relationship that exists when a class inher-its from another class.

True

T/F When a method is declared with the final modifier, it cannot be overridden in a subclass.f a subclass attempts to override a final method, the compiler generates an error.

True

T/F When a subclass extends a superclass, the public members of the superclass become public members of the subclass.

True

T/F e. Private members of the superclass cannot be accessed by the subclass, so technically speaking, they are not inherited. When an object of the subclass is created, the private members of the superclass exist in memory, but only methods in the superclass can access them. They are truly private to the superclass.

True

True or False: A superclass has a member with package access. A class that is outside the superclass's package but inherits from the superclass can access the member.

True

True or False: A superclass reference variable can reference an object of a subclass that extends the superclass.

True

With this type of binding, the Java Virtual Machine determines at runtime which method to call, depending on the type of the object that a variable references.

dynamic

When a class contains an abstract method, you cannot create an ___________of the class.

instance

There is an operator in Java named_________ that you can use to determine whether an object is an instance of a particular class.

instanceof

a class can extend only _____ superclass, but Java allows a class to implement multiple interfaces.

one

True or False: By default all members of an interface are public.

True

True or False: In a subclass, a call to the superclass constructor can only be written in the subclass constructor.

True

True or False: The superclass constructor always executes before the subclass con-structor.

True

True or False: When a class contains an abstract method, the class cannot be instantiated.

True

If a subclass constructor does not explicitly call a superclass constructor, Java will automatically call the superclass's default constructor, or no-arg constructor, just________ the code in the subclass's constructor executes

before

Beginning in Java 8, interfaces may have default methods. A default method is an interface method that has a__________.The method header begins with the key word default.

body

Fields in an interface are __________.

both final and static

The relationship between a class and an interface is known as a realization relationship (the class realizes the interfaces). You show a realization relationship in a UML diagram by connecting a class and an interface with a ___________ line that has an open arrowhead at one end. The arrowhead points to the interface. T

dashed

When an abstract method appears in a class, the method must be overridden in a subclass. If a subclass fails to override the method, an ______ will result. Abstract methods are used to ensure that a subclass implements the method.

error

The @Override annotation in line 30 is not required, but it is recommended that you use it. If the method fails to correctly override a method in the superclass, the compiler will display an _______________

error message.

Abstract classes cannot __________.

be instantiated

A functional interface is an interface with __________.

only one abstract method.

T/F it is not possible for a superclass to call a subclass's method.

True


Conjuntos de estudio relacionados

Chapter 3: Lifting and Moving Patients [Pre/Post Test]

View Set

Domain II: Healthcare and Simulation Knowledge/Principles (CHSE Review Book)

View Set

AP Macroeconomics Remote Learning Week 6

View Set

Engineering Materials Chapter 10: Fundamentals of metal casting

View Set