Sathya Krish wrote:In what way does interface and implementation works.
Sathya Krish wrote:if i have an interface, can i have more java programs having the main statement but using the same interface
Sathya Krish wrote:In what way does interface and implementation works.
if i have an interface, can i have more java programs having the main statement
but using the same interface
dR
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Here's the rub. In the context of this example, an interface is just a set of method signatures. These methods are implemented in the implementing class. So the clients know that the classes contain methods with these signatures. So what exactly is it that is being hidden? If the interface did not exist, then clients are still accessing the classes via methods, which entails knowledge of the signatures, which they would have had anyways with the interface, so it's not at all clear to me how not implementing the interface would force the first company to reveal any more about their implementation than they already are revealing with the interface. In both cases, all the client knows/needs to know is the signature.
Sun goes on to claim that this enables the first company to keep secret the details of proprietary implementations of their classes.
Henry Wong wrote:
Here's the rub. In the context of this example, an interface is just a set of method signatures. These methods are implemented in the implementing class. So the clients know that the classes contain methods with these signatures. So what exactly is it that is being hidden? If the interface did not exist, then clients are still accessing the classes via methods, which entails knowledge of the signatures, which they would have had anyways with the interface, so it's not at all clear to me how not implementing the interface would force the first company to reveal any more about their implementation than they already are revealing with the interface. In both cases, all the client knows/needs to know is the signature.
Knowing the interface doesn't mean that you know the implementation details. For example, lets say you have a List collection, it could be implemented via an array ArrayList, it could be implemented as a linked list, it could be thread safe (or not). The fact is, you don't care. Your code is passed a List, you used the list via its interface, and your code will will work fine with any call that implements the list.
The implementation is hidden. And because of it, your code can work with multiple types of collection classes.
Henry
What I am asking is how is it that the lack of an interface reveals the implementation? See the difference?
Henry Wong wrote:
What I am asking is how is it that the lack of an interface reveals the implementation? See the difference?
I think you are misinterpreting the document. Using a interface does indeed "hide" the implementation, but that does not mean that the implementation is hidden. It just means that it is abstracted, so that the client don't have to care about it. It doesn't mean that the implementation is secure from the client.
Basically, not using an interface will tie you to an implementation. Meaning that if you choose the ArrayList, your code won't work with linked lists, or vectors, or synchronized lists, etc.
Henry