Interfaces & Inner Classes Flashcards
An interface is not a class.
True.
Interfaces contain method definitions.
False - they usually only contain method headings.
Interfaces can contain constant definitions.
True.
Interface can never give a definition to its own method.
False - default methods (as of Java 8) have definitions. But generally, no.
Interfaces solve which Java restriction?
Java’s restriction on multiple inheritance.
An interface and all of its method headings should be declared public.
True.
An interface can give private/protected/package access to methods in specific circumstances.
False.
When a class implements an interface, that class can decide to make specific methods private.
False - they must all remain public.
A method may be written with a parameter of an interface type.
True - this parameter accepts any class that implements the interface.
Method headings in interfaces must end in a semicolon.
True.
Write a class header for class “C” that implements 3 interfaces (I1, I2, and I3).
public class C implements I1, I2, I3 {…}
How many of the methods listed in the interface(s) definition(s) must be defined by the class implementing them?
All methods listed must be implemented.
Abstract classes cannot implement interfaces.
False - they may implement 1+ interfaces.
Abstract classes must define all methods listed in the interface(s) they implement.
False - abstract classes do not need to provide implementation - this is left for the concrete class to do.
Interfaces can be derived from other interfaces.
True.
Write the header for an Interface “I1” that extends another interface, “I2”.
public interface I1 extends I2 {…}
Variables defined in an interface must be public, static, and final.
True.
The public, static, and final modifiers must precede all variables defined in an interface.
False, they may be omitted since the compiler understands that they must already be public, static, and final.
What are the 2 possible cases of inconsistent interfaces?
Constants with the same name, but different values, and methods with the same name, but with different return types.
The Serializable interface is completely empty.
True.
The Cloneable interface is completely empty.
True.
Object.clone() does a bit-by-bit copy of the object’s data in storage. This is adequate behaviour for what types of Objects?
For immutable class types.
What Exception is thrown when the Object’s clone() method is called on an instance that does not implement cloneable?
CloneNotSupportedException
CloneNotSupportedException is a checked exception.
True.
How do you implement Cloneable for a class with mutable class type attributes?
Invoke clone() of the base class, then reset the values of mutable instance variables by invoking their respective clone methods.