In the hash table section of the algorithm, the following method is required to implement the hash list API with the Zipper method:
Public iterable<key> keys ()
We know that iterable is an interface, so how does a method return an interface? In "Effective Java", 52nd is "referencing objects through interfaces"
As parameter types. More generally, you should favor the use of interfaces rather than classes to refer to objects. If appropriateInterfaceTypes exist, then parameters,returnValues, variables, and fields should all is declared usingInterfaceTypes. The only time you really need to refer to an object ' sclassis when you ' re creating it with a constructor. To make ThisConcrete, consider the Caseof Vector, which is an implementation of the ListInterface. Get in the habit of typing This://good-uses interface as typelist<subscriber> subscribers =NewVector<subscriber>(); rather than This://bad-uses class as type!vector<subscriber> subscribers =NewVector<subscriber>If you get into the habit of using interfaces as types, your program would be much more flexible. If you decide this want toSwitchImplementations. Dois change theclassName in the constructor (or use a differentStaticfactory). For example, the first declaration could is changed to ReadList<Subscriber> subscribers =NewArraylist<subscriber>(); and all of the surrounding code wouldContinueTo work. The surrounding code is unaware of the old implementation type, so it would is oblivious to the change. There is one caveat:ifThe original implementation offered some special functionality not required by the general contract of theInterfaceAnd the code depended on this functionality, then it's critical thatNewimplementation provide the same functionality. It is entirely appropriate to refer to an object by aclassRather than anInterface ifNo appropriateInterfaceExists. For example, consider value classes, such as String and BigInteger. Value classes is rarely written with multiple implementations in mind. They is oftenFinaland rarely have corresponding interfaces. It is perfectly appropriate to use such a valueclassAs aparameter, variable, field, orreturnType. More generally,ifA concreteclasshas no associatedInterfaceAnd then you had no choice but to refer to itclassWhether or not it represents a value. The RandomclassFalls into Thiscategory. A Second CaseIn which there is no appropriateInterfaceType is, the objects belonging to a framework whose fundamental types is classes rather than interfaces. If an object belongs to such aclass-based framework, it is preferable to refer to it by the relevant baseclass, which is typicallyAbstract, rather than by its implementationclass. The Java.util.TimerTaskclassFalls into Thiscategory. AFinal CaseIn which there is no appropriateInterfaceType is, that's classes that implement anInterfaceBut provide extra methods not found in theInterface— forExample, Linkedhashmap. Such AclassShould are used to refer to their instances onlyifThe program relies the extra methods. It should rarely be used as a parameter type (Item 40). These cases is isn't meant to being exhaustive but merely to convey the flavor of situations where it's appropriate to refer to a object by itsclass. In practice, it should is apparent whether a given object have an appropriateInterface. If it does, your program would be is more flexibleifYou use theInterfaceTo refer to the object;ifNot, just use the least specificclassIn theclasshierarchy that provides the required functionality. Reference:effective Java 2nd Edition by Joshua Bloch
Subclasses are able to move up to the parent class, so the message subclasses passed to the parent class are acceptable, and the interface and its implementation class have a somewhat different kind of relationship, of course. When we return an interface type, it actually means that you can then assign its return value to an instance of an implementation class that implements this interface. The benefit of this writing is that an instance of the implementation class for this interface can be arbitrarily specified at run time without the need to modify the code of the interface function. If a suitable interface type exists, then for parameters, return values, variables, and domains, the interface type should be used for declaration. If there is no suitable interface, you can use the class instead of the interface to refer to the object.
View the collections related libraries of the JDK you can find a lot of referencing objects using the return interface, such as iterator () the implementation of these methods is to return a interface, actually only need to implement this interface, Implementing interator for different collections does not affect the invocation of iterator.
Back to the beginning public iterable<key> keys () How to do this, the use of ArrayList to implement, of course, can also return any other implementation of the Iterable interface class:
Public Iterable<key> keys () { ArrayListnew arraylist<key>(); for (int i = 0; i < M; i++) { Ka.addall (St[i].keys ()); } return ka; }
The interface in Java is returned as a method