Defining and Implementing an Interface

     

An interface is a set of requirements to which classes must conform if they want to use the service provided by the interface. To implement an interface, first declare that your class will implement the given interface; second, define the methods in the interface.

java.util.Collection has an interface that defines, among other methods, an add() method. This ensures that any implementation of java.util.Collection will support its methods.

An interface is defined as follows :

 

 public interface Collection { public abstract boolean add(A o) ... } 

An interface is implemented as follows:

 

 public class myClass implements Collection { //code here. can write, for instance, add() since //add() is a method of java.util.Collection, and //this class implements that interface. Eventually //all methods in Collection must be implemented... ... } 



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

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