• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

interface and implementation

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to all of you for your valuable advices
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic