Chapter 10 Inheritance (Reading)
The ________ annotation 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.
@Override
10.23 What is the purpose of an interface?
To specify behavior for other classes.
10.16 Look at the following class definition: public class ClassD extends ClassB { (Member Declarations . . .) } Because ClassD inherits from ClassB, is it true that ClassD does not inherit from the Object class? Why or why not?
ClassD still inherits from Object, because all classes ultimately inherit from Object.
The "Is-a" Relationship ___ _____Work in Reverse
Does not
A protected member's _____ is somewhere between private and public.
access
An ______ inner class is an inner class that has no name.
anonymous
An ______ inner class must implement an interface, or extend another class.
anonymous
If a subclass constructor does not explicitly call a super class constructor, Java will ______ call the superclass's default constructor, or no-arg constructor, just before the code in the subclass's constructor executes.
automatically
superclasses are also called ____ _____
base classes
Members with package access, however, _______ be accessed by subclasses that are in a different packag
cannot
The subclass inherits ____ _____ ____ from the superclass without any of them having to be rewritten
fields and methods
all fields in an interface are treated as ___ ____ ___
final and static.
In the Object class, the toString method returns a reference to a String containing the object's class ____, followed by the ___ sign, followed by the object's ____ _____, which is a hexadecimal number.
name, @, hash code
You use the ______operator to simultaneously define an anonymous inner class and create an instance of it.
new
If a functional interface's abstract method has no parameters, any lambda expression that you use with the interface must also have _____ parameters.
no
You can optionally write public in the method header, but most programmers leave it out because all interface methods must be _______.
public
10.27 Write the first line of a class named Customer, which implements an interface named Relatable.
public class Customer implements Relatable
A subclass may have a method with the same _______ as a superclass method. In such a case, the subclass method overrides the superclass method.
signature
You can think of the ______ as an extended version of the superclass.
subclass
_______ allows a new class to extend an existing class. The new class inherits the members of the class it extends.
Inheritance
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 and abstract classes are defined with the ________ key word.
abstract
subclasses are also called ___ ____
derived classes
If a functional interface's abstract method has multiple parameters, any lambda expression that you use with the interface must also have ________parameters.
multiple
You can think of a lambda expression as an anonymous method, or a method with no _______.
name
To specify ___ ____ in a class definition, simply list the names of the interfaces, separated by commas, after the implements key word.
multiple interfaces
If a super class does not have a default constructor and does not have a no-arg constructor, then a class that inherits from it ___ _____ one of the constructors that the superclass does have.
must call
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
A protected member is not quite ______, because it may be accessed by some methods outside the class.
private
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 _____ to the superclass.
private
Protected members are not quite ____ either because access to them is restricted to methods in the same class, subclasses, and classes in the same package as the member's class.
public
10.28 Write the first line of a class named Employee, which implements interfaces named Payable and Listable
public class Employee implements Payable, Listable
When a method overrides another method, however, they both have the ____ _____.
same signature
Abstract methods have no body, and their header must end with a ________.
semicolon
You can write multiple ________ in the body of a lambda expression, but if you do, you must enclose the statements in a set of curly braces, and you must write a return statement if the expression returns a value.
statements
In order to call the overridden superclass method, we would have to use the _______ key word in the subclass method.
super
An _____ class is a class that is defined inside another class.
inner
The _________ operator understands the "is-a" relationship that exists when a class inherits from another class.
instanceof
An abstract class is not ________itself, but serves as a superclass for other classes.
instantiated
The _____key word refers to an object's superclass and can be used to access members of the superclass.
super
You can use the _____ key word to call a superclass constructor.
super
The super key word refers to an object's ______.
superclass
When a class contains an abstract method, it cannot be instantiated. It must serve as a ____
superclass
Because every class directly or indirectly extends the Object class, every class inherits the Object class's members. Two of the most useful are the ___ _____ _____ methods.
toString and equals
If a functional interface's abstract method is void (does not return a value), any lambda expression that you use with the interface should also be _____.
void
An ________ method has no body and must be overridden in a subclass.
abstract
In its simplest form, an interface is like a class that contains only _______methods.
abstract
10.7 How can a subclass method call an overridden superclass method?
A subclass may call an overridden superclass method by prefixing its name with the super key word and a dot (.).
10.19 What is the purpose of an abstract method?
Abstract methods are used to ensure that a subclass implements the method.
10.22If a class is defined as abstract, what can you not do with the class?
An abstract class cannot be instantiated. It must serve as a superclass.
10.21What is the purpose of an abstract class?
An abstract class serves as a superclass for other classes. It represents the generic or abstract form of all the classes that inherit from it.
10.25 How is an interface different from an abstract class, or any class? .
An interface only specifies methods; it does not define them. In addition, all members of an interface are public.
10.10 How do you prevent a method from being overridden?
You declare the method with the final modifier.
10.26 If an interface has fields, how are they treated?
As final and static.
10.13Why should you avoid making class members protected when possible?
Because any other class that inherits from the class, or is in the same package, has unrestricted access to the protected member.
10.17 When you create a class, it automatically has a toString method and an equals method. Why?
Because those methods are members of the Object class.
10.24 How is an interface similar to an abstract class?
It cannot be instantiated.
10.9 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?
It overloads the superclass method.
10.8 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?
It overrides the superclass method.
The Java API has a class named ________, which all other classes directly or indirectly inherit from.
Object
When a class does not use the extends key word to inherit from another class, Java automatically extends it from the ________ class.
Object
every class has a toString and an equals method, and now you know why! It is because those methods are inherited from the _____ class.
Object
10.20If a subclass extends a superclass with an abstract method, what must you do in the subclass?
Override the abstract method.
10.12What is the difference between private members and protected members?
Private members may be accessed only by methods in the same class. A protected member of a class may be directly accessed by methods of the same class or methods of a subclass. In addition, protected members may be accessed by methods of any class that are in the same package as the protected member's class.
10.14What is the difference between private access and package access?
Private members may be accessed only by methods in the same class. Any method in the same package as the class may directly access the class's members that have package access.
_______members of a class may be accessed by methods in a subclass, and by methods in the same package as the class.
Protected
10.11 When a class member is declared as protected, what code may access it?
Protected members of class may be accessed by methods in a subclass, and by methods in the same package as the class.
10.6 Under what circumstances would a subclass need to override a superclass method?
When the superclass method is inadequate for the subclass's purpose.
10.15 Why is it easy to give package access to a class member by accident?
When you accidentally leave out the access specifier, the member will have package access.
In the Object class, the _____ method accepts a reference to an object as its argument. It returns true if the argument references the calling object.
equals
superclass constructor always ____ before the subclass constructor
executes
The term ______ means the ability to take many forms.
polymorphism
When a subclass extends a superclass, the public members of the superclass become public _____ of the subclass.
members
If a superclass has either (a) a default constructor or (b) a no-arg constructor that was written into the class, then that constructor will be automatically called just _______a subclass constructor executes.
before
If a method in a subclass has the samesignature as a method in the superclass, the subclass method ______ the superclass method.
overrides
When a class contains an abstract method, you _______ create an instance of the class. Abstract methods are commonly used in abstract classes.
cannot
To use more than one parameter in a lambda expression, simply write a ___ - _____ list and enclose the list in parentheses
comma-separated
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
If the anonymous inner class extends a superclass, the super class's no-arg constructor is called when the object is ______.
created
Abstract methods are used to ensure that a subclass _________ the method.
implements
When you want a class to implement an interface, you use the _____ key word in the class header
implements
Private members of the superclass cannot be accessed by the subclass, so technically speaking, they are not ________.
inherited
Because fields automatically become final in an interface you must provide an ______ value.
initialization
In Java, a reference variable is _______ because it can reference objects of types different from its own, as long as those types are subclasses of its type.
polymorphic
In an inheritance relationship, the subclass inherits members from the superclass, not the other way around. This means it is _____ _______ for a superclass to call a subclass's method.
not possible
a class can extend only _____ superclass, but Java allows a class to implement multiple interfaces.
one
The super statement that calls the superclass constructor may be written ______ in the sub-class's constructor. You cannot call the superclass constructor from any other method.
only
You already know that _____ methods can appear within the same class.
overloaded
An abstract method must be _______ in a subclass.
overridden
An anonymous inner class must _____ all of the abstract methods specified by the interface it is implementing, or the superclass it is extending.
override
When a class implements an interface, the class must _______the methods that are specified by the interface.
override
a method cannot _______ another method in the same class.
override
Because the subclass is more specialized than the superclass, it is sometimes necessary for the subclass to replace inadequate superclass methods with more suitable ones. This is known as method ______.
overriding
You do not have to specify the ____ ____ of a lambda expression's parameter because the compiler will determine it from the interface's abstract method header. However, you can explicitly declare the data type of a parameter, if you wish.
data type
Protected members may be accessed by methods in the same package or in a subclass. This is true even if the subclass is in a ____ ______.
different package
Although overloaded methods have the same name, they have ___ ________.
different signatures
Java performs ______ binding or late binding when a variable contains a polymorphic reference. This means that the Java Virtual Machine determines at runtime which method to call, depending on the type of object that the variable references. So, it is the ___ ____ that determines which method is called, not the vari-able's type.
dynamic, object's type
An _______ final variable is a variable whose value is never changed, but it isn't declared with the final key word.
effectively
lambda expression can access variables that are declared in the enclosing scope, as long as those variables are final, or _______ final.
effectively
When a method is declared with the ____ ____, it cannot be overridden in a subclass.
final modifier
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
A _______ interface is simply an interface that has one abstract method.
functional
Ananonymous inner class must either _____ an interface, or _____ another class.
implement, extend
An interface cannot be instantiated. Instead, it is __________ by other classes.
implemented
There is an operator in Java named _______ that you can use to determine whether an object is an instance of a particular class.
instanceof
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
When an "__ ____" relationship exists between objects, it means that the specialized object has all of the characteristics of the general object, plus additional characteristics that make it special.
is a
When one object is a specialized version of another object, there is an "__ __" relationship between them.
is a
the lambda expression begins with a parameter variable, followed by the _____ _______, followed by an expression that has a value.
lambda operator (->)
Because ananonymous inner class's definition is written inside a method, it can access that method's ___ ______, but only if they are declared final, or if they are effectively final.
local variables