interfaces and inner classes Flashcards
what is an interface
extreme case of an abstract class not a class a type that can be satisfied by any class that implements that interface
what does an interface do
specifies a set of methods that any class that implements the interface must have
what does an abstract class have
- contains method headings and constant definitions only
how does java approximate multiple inheritance
through interfaces
how do methods with interface parameter types work
that parameter will accept as an argument any class that implements the interface
how must a concrete class implement an interface
implements InterfaceName
must implement all the method headings listed in the definitions of the interfaces
abstract class implementing interfaces
may implement one or more interfaces
Any method headings given in the interface that are not given definitions are made into abstract methods
A concrete class must give definitions for all the method headings given in the abstract class and the interface
derived interfaces
extends BaseInterface
A concrete class that implements a derived interface must have definitions for any methods in the derived interface as well as any methods in the base interface
interface pitfall
runtime and compiler systems dont check that the interface body is consistent with the intended meaning
If the method body does not satisfy the specified semantics, then software written for classes that implement the interface may not work correctly
what rules must total ordering satisfy?
irreflexivity - for no object o does o come before o
Trichotomy - for any objects o1 and o2 only of of the following are true: o1 comes before o2, o1 comes after o2, o1 == o2
transivity - if o1 comes before o2 and o2 comes before o3 the o1 comes before o3
what inconsistencies happen when a class implements two interfaces
One type of inconsistency will occur if the interfaces have constants with the same name, but with different values
Another type of inconsistency will occur if the interfaces contain methods with the same name but different return types
when will the class definition using more than 1 interface be illegal?
implements two inconsistent interfaces
what is the serializable interface?
- no method headings or constants - completely empty
- used merely as a type tag that indicates to the system that it may implement file I/O in a particular way
what is the cloneable interface
no method heading or contained constants
used to indicate how the method clone (inherited from the Object class) should be used or redefined
when implementing Cloneable for primitive or immutable classes
just use the existing Object.clone