Interface Instances as Input Parameters and Return Types


Interfaces are different from classes in that you cannot create an instance of an interface. Interface reference types can be used, however, both as input arguments and return types. This may sound odd, but it provides a lot of functionality and flexibility when creating your methods . An instance of a class is also considered an instance of any interface it implements. A method that lists an interface type as an input parameter can take as an argument any object that implements the interface.

This is commonly used for methods that are applicable to a variety of classes in different class hierarchies. Because there may not be a single parent class for all the applicable classes, a method can define an interface type as an input parameter instead. The method can then accept a variety of object types as an argument rather than just members of a given class hierarchy. The same is true when a method defines an interface type as its return type. The method can return an instance of any class that implements the interface.

Example: Using Interfaces to Reference Disparate Classes

Java defines a variety of collection classes in the java.util package, many of which can work together because they all implement a set of common interfaces. In this example, Vector and ArrayList objects are created and loaded with data. We next want to put the contents of these objects into a LinkedList . We can do this with the following method from the LinkedList class ”

 public boolean addAll(Collection c) 

The Collection interface is defined in the java.util package. Because the Vector and ArrayList classes both implement the Collection interface, instances of the Vector and ArrayList classes can be passed to this method.

Interface instances can also be used as the return type from methods. The LinkedList object calls the listIterator() method that returns an instance of the ListIterator interface. This instance can be used to iterate through the elements of the LinkedList .

 import java.util.*; public class ArgDemo {   public static void main(String args[]) {     //  A Vector and ArrayList object are created and     //  filled with some elements.     Vector vector = new Vector();     vector.add("Jackson");     vector.add("Zachary");     ArrayList arrayList = new ArrayList();     arrayList.add("Mark");     arrayList.add("Maria");     //  A LinkedList object is created     LinkedList linkedList = new LinkedList();     //  The contents of the Vector and ArrayList are     //  added to the LinkedList using the addAll()     //  method. Because the method takes a Collection     //  instance as an argument, it can accommodate many     //  different types of input arguments.     linkedList.addAll(vector);     linkedList.addAll(arrayList);     //  The listIterator() method returns an object that     //  implements the ListIterator interface. This     //  object can be used to iterate through the linked     //  list.     ListIterator iterator = linkedList.listIterator();     while( iterator.hasNext() ) {       System.out.println( iterator.next() );     }   } } 

Output ”

 Jackson Zachary Mark Maria 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net