This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Ok i understand all the rules of interfaces and that they help to with javas no existence of multiple inheritance. But i dont understand why this is so or why they are neccessary or an example in which you would use them.
Interfaces is a big key to Polymorphism. You can code to an interface and never need to know exactly which implementation class you are using. So if you have three possible implementation that depend on who is calling it. Instead of writing three methods each to take a different implementation, you can write one method that takes the interface and calls interface methods to produce your results.
It helps quite a bit in a lot of Design Patterns to have this functionaily.
There are some interfaces that are needed as "Marker" interfaces like Serializable, so that it is known that this class can be passed over RMI, etc.
To expand on Mark's explanation, you can store objects of different types in a collection if they all implement the same interface. The collection itself would be a collection of references to the interface type, not the object type. That's a powerful tool!
For example, if you have an interface called "drivable" that has one method, drive, you could have the following classes implement drivable: car, bicycle, motorcycle. Then, you could have collection of drivable objects and iterate through the collection calling the drive method on each object within the collection without having to worry about what type the object is. By each object having implemented the drivable interface, the contract guarantees that each object in the collection of drivables will implement the drive method.
Hope that helps...
Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)