Hi Meadowlark_B,
Interfaces ?? ah! it takes time and patience to understand them. it did take time for me.
Part I:
Before 1.2 Collections u had Vector, and HashTable. How did u add and element to them. U used addElement for vectors and put for Hashtable. do u remember these when asked randomly in a future time. NO!!! u don't.
Now comes Collection interface. u know that all classes implementing collection interface have an add method. thus whether they be Hashset or LinkedList or ArrayList u know add is there. thus u use add in any collection.
Keeps things simple isn't it.
Part II.
Makes OOPS easier and posible. Let me give u an example
Collection i=new LinkedList();
i.add("X");
i.add("y");
i.add(z");
....
now in a later date i realize that Oh! i have made a mistake i should have used a HashMap instead. what do i do?.
change one line of code
Collection i= new HashMap();
that's it. everything else works perfectly. No other modifications are required. compare this with if i wanted to use a HashTable. ah!!! i have to remove all "add", and replace them with "put". make revelent changes to each and every place where some methods are invoked. compile and debug code all over again. I would die!!!.
Part III.
Interfaces are contracts as u already know. the implementing classes must implement the same signatures and return type.
This is checked at compile time itself not at runtime. This is useful to the developer as any errors he makes are known before hand itself. thus if a class implements a WindowListener then if any of the abstract methods are missing then the compiler complains. The methods may be missing as the developer has not declared them or declared them wrongly.
Part IV
I believe that Java is nothing but interfaces. Sun is constantly giving us interfaces be it for
JDBC,
EJB or other areas. Vendors implement these interfaces to gives us the needed platform independence and vendor independence. thus one program created by JDBC on Oracle can be used on any other RDBMS just by changing the driver.
Part V
A programer too can use these interfaces to create classes that r specific to his needs. Another user can use these classes easily as he knows that they implement these interfaces.
for example u can create u r own collection class implementing the collection interface and give it to someone else to use. The user does not need to struggle to find out how to use as he already knows the add() method of collection or the remove() method.
Thus the implementation has changed but not the interface and causes no consequential changes to other code.
Hope i have clarified things abundantly. Do post for further queries.
Regds.
Rahul.
[This message has been edited by rahul_mkar (edited July 16, 2000).]