GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes interface and implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interface and implementation" Watch "interface and implementation" New topic
Author

interface and implementation

Sathya Krish
Greenhorn

Joined: Aug 20, 2009
Posts: 8
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

Joined: Dec 25, 2006
Posts: 60
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

Joined: Jul 28, 2009
Posts: 45
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
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

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.


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Raghu Devatha
Ranch Hand

Joined: Feb 10, 2009
Posts: 39
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.

dR
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

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".


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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

Joined: May 13, 2009
Posts: 679
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
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

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

Joined: May 13, 2009
Posts: 679
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

Joined: Aug 20, 2009
Posts: 8
thanks to all of you for your valuable advices
 
GeeCON Prague 2014
 
subject: interface and implementation