I would like to know, what is the best way to make full use of Interfaces, don't tell me that they support multiple Inheritance, i want to know how should i use them in Object oriented programming and why.
<a href=http://www.coderanch.com/t/405075/java/java/Reusabilty-code-lack-interfaces-polymorphism>check this thread too </a>
Quite simply, interfaces should be used whenever two classes share a commonality, but the commonality does not suffice the 'is-a special type of' commonality expressed through inheritance.
Whever there is commonality, like a bird and a boat delivering mail, you should recognize that commonality, and have those classes implement a common interface. "factoring out interfaces" is a common activity when having completed a class diagram of a problem domain.
when you apply the principle of programming to an interface, not an implementation, in Java, you will use interfaces for most non-primitive types in your program. They can occur as a type in a declaration, as a method parameter type or a return type.
Here are some examples:
Whatever the context of these two lines is, in most cases it would be best if Dog, Owner and Possession were interfaces.
If for example Possession were not an interface but a class, then the public method would depend on and be bound to a certain implementation. This is a problem in itself already which can have very concrete disadvantages. It would also be a symptom which often indicates that the program is not really object oriented, which means that it does not base its primary logic on encapsulation, inheritance and polymorphism.
The concrete damage is a lot less severe for APIs which are not published to anyone, not even to a single co-worker. If class types are used often, it still is a symptom for severe design problems.
I wrote a lot about this very point in my Design Principles article. The sections "Consequences" and "Common justifications for not using an interface type" elaborate more on this.
Kai [ October 17, 2006: Message edited by: Kai Witte ]