Originally posted by johnson david:
Hi friends
The fallowing are the uses of interfaces in java, can somebody explain me these two thigs
1) Capturing similarities between unrelated classes without forcing a class relationship
2) Revealing an object's programming interface without revealing its class
thanx in advance
regards
For part 1, when a class definition implements an interface, there is an "is a" relationship.
This means that two classes that are completely different can have an "is a" relationship with the same interface, and can be sent as parameters to methods that accept the interface type.
An example would be Comparable. You can have many diverse classes that implement the Comparable interface. But if you create a sort method whose parameter type is Comparable, then you can send two different objects created from classes that don't really have anything else in common.
For part 2, an interface only contains inteface constants and abstract methods. This means that you see the methods that must be contained in the class if it implements the interface simply by looking at the abstract methods in the interface definition.