Chapter 20: Interfaces, Including Generic Interfaces Flashcards
How can a type parameter extend an interface?
Bind the type parameter so that the type may be a class that implements the interface, the interface itself or an interface that extends it
A class is a type and has three components:
Set of values (set of objects that are instances of the class)
Operations that can be performed on those values (instance methods)
Operation interfaces to those operations (method interfaces)
How does binary search work?
If items are sorted in a known total order.
Two indices, high and low.
look half way, if less low = middle, if high high = middle, if found found.
If low and high meet, not present
What is a total order?
A relationship between pairs of data that enables it to be sorted.
Anti symmetric, transitive, total
Describe multiple inheritance. What is the problem?
I can appear natural to view a class as a subclass of more than one superclass, inheriting properties from each superclass
Problem:
Ambiguity (if two superclasses contain a method of the same signature)
Run time efficiency (dynamic method binding- would have to search all trees to find method)
What is an interface? What are the rules?
Like a class except all instance methods are abstract and only method interfaces are declared (signatures and return types)
Rules: contains a list of instance method heading with no body All instance methods public Cannot contain constructor methods No class methods No private instance methods or variables
How is interface a type?
Set of all references to objects that are instances of a class that implements interface operations are methods implementations of instance methods Operation interfaces is the method interface defined in the interface
How do we implement a method?
A non abstract class that implements an interface must supply method implementations for the abstract methods defined in the interface
What is a generic interface?
An interface with type parameters. Type parameters may be used as types in the declaration of abstract methods
How can an interface extend an interface?
An interface can extend another (and many other) interface(s). Abstract methods and class constants are inherited.
Can a class implement many interfaces?
Yes. List with commas after “implements”
What is a generic method?
A method with type parameters <> before return type. May be defined in generic or non generic classes. May be class or instance methods- more useful as class methods. class.mymethod(paramters)
What class methods does java.util.Arrays provide?
Sort- takes array of objects that it sorts into natural ordering. Items must be comparable.
Copy of- generic method. Takes array and new length
What is comparable?
If a class implements comparable, the class provides compare to enabling objects to compare with a given other. compareTo provides natural ordering
What is the relationship between compare to and equals?
equals should always be the same as compareTo(x) == 0