Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface and implementation

 
Sathya Krish
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sujith Acharya
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
implementing interface is like signing a contract. If a class(concrete class) implements an interface, then it should have implementation of methods declared in the interface.
You can have many java class which implements same interface with main method in it.
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Sujith said above interfaces are like contracts. If a certain interface is implemented by any class, it indicates that a class supports certain features which are mentioned in the interface/contract. Hence a user can be sure if a class implements a interface it is sure to provide those features/methods because the compiler ensures that the methods inherited from the interface are overriden in the class implementing it.

Some interfaces may not specify any methods any methods but a class may have to implement them so that it can use certain features. These are called marker interfaces. For example 'Serializable' interface indicates that the objects of the class can be serialized.

You can't create a object of the interface but can create a reference to it. And it can refer to the object of a class which implements it, to call the methods declared in the interface. Doing that means we are using polymorphism to call those methods.

And yeah we can implement multiple interfaces in a class unlike class inheritance.

Hope that made it clear.

 
salvin francis
Bartender
Posts: 1277
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sathya Krish wrote:In what way does interface and implementation works.

An interface provides a standard for implementation.

Sathya Krish wrote:if i have an interface, can i have more java programs having the main statement but using the same interface

This is not aim of an interface, however its possible to use both interfaces and classes in more than one program.
 
Raghu Devatha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sathya Krish wrote:In what way does interface and implementation works.


You have the correct answers above.

if i have an interface, can i have more java programs having the main statement
but using the same interface


Yes you can. But the concept of Interface/Implementation do not directly relate to this.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
believe it or not, you can have a program that uses dozens of classes... and it's possible that each and every class will have a "public static void main(String [] args)" in it.

However, the only one that will run is the one called with the "java XXX" command".
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May I piggyback on this thread? it's just another aspect of the exact same topic.

OK, I have difficulties in seeing just how it is that interfaces deliver some of the benefits that Sun claims they deliver. For example, this page http://java.sun.com/docs/books/tutorial/java/IandI/createinterface.html contains an example of a company that writes image processing classes that are sold to customers who create graphics packages. The company writes these classes to interfaces, and these interfaces (APIs) are made public to the clients who code to these interfaces.Sun goes on to claim that this enables the first company to keep secret the details of proprietary implementations of their classes.

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.

Is my question clear?
 
Henry Wong
author
Marshal
Pie
Posts: 21202
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21202
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, this part...

Sun goes on to claim that this enables the first company to keep secret the details of proprietary implementations of their classes.


is just silly. Hiding implementations doesn't mean that it is secure. Hiding implementation means that it is a level of abstraction that enable code to work with multiple types of implementation that support a particular contract.

Henry

 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


I understand what you say, but it's not really my point. I don't question that an interface hides the implementation. What I am asking is how is it that the lack of an interface reveals the implementation? See the difference? What else is being revealed besides the method signature, which the interface is revealing anyway.
 
Henry Wong
author
Marshal
Pie
Posts: 21202
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


I actually missed your first comment above about what "hiding an implementation" really means. That post, and this one that I reply to, helps a lot. Yes I agree, it seems I just misunderstoof the definition of hiding.

Thanks again.
 
Sathya Krish
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all of you for your valuable advices
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic