Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Differences between interfaces and implementations

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good evening everyone,

I'm studying Java Collection Framework.
It is composed by interfaces and its implementation.
I give you an example which will bring to my doubt.

In JCF we can find:
The interface List and its implementation ArrayList.
I can create a new collection of objects writing:

Collection<String> names = new ArrayList<>;

Can anyone explain me the difference between an interface and its implementation, and, being more spefic:
what is an implementation of an interface?
 
Marshal
Posts: 68907
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The interfaces specify what the classes will do.
The classes decide how they are going to do it.

Maybe, have a look at this FAQ and see if that doesn't help you. If not, ask again.
 
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also look at this way: an interface is a "specification" whereas an implementation is a particular execution of that specification.

For example, The Java Language Reference is a specification for how Java works. OpenJDK is an implementation of that specification. Oracle has its own implementation of the specification. Somebody else can create their own implementation. You'd have one specification with possible multiple implementations but all implementations need to work according to the specification if they are to be considered correct and proper Java.

Another example, this one of a somewhat more informal "interface": The phone. A phone will have some way of dialing a telephone number. At the very least, you'd have ten digits, '0' to '9', that you can combine into a number. When you dial those numbers there's some magic that goes on that connects your phone to another phone somewhere in the world and allows you to talk to the person on the other phone. That's the minimal "phone" interface we have all come to expect. There are many different implementations of this interface. I have a Samsung Note. My kids have Apple IPhones. I also have an IP Phone on my computer and an older wireless phone in a box in the basement. I'm old enough to remember the old rotary phones that you could actually dial instead of press buttons. All these are different implementations of the same "phone" interface.
 
Claud Mann
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!!
Staff note (Junilu Lacar):

@OP: Please don't quote entire posts if they're more than just one or two sentences long. Quoting long posts makes the thread more difficult to read. Your reply has been edited accordingly.

 
Campbell Ritchie
Marshal
Posts: 68907
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
Ranch Hand
Posts: 528
10
Android Open BSD Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claud Mann wrote:
Can anyone explain me the difference between an interface and its implementation, and, being more spefic:
what is an implementation of an interface?



As you are studying Collections I understand your doubts, and I suggest you to read a book recommended by almost everybody on this forum "effective Java" is an advanced book but explain really well the way Collection interface work.
It has some layer of complexity, my suggestion is you focus on the concept you ask before, and as you ask

An interface is a contract, means if you have a class Dog, a class Cat, and a class Duck, to be sure that they are going to emit Sound, you are going to implement the Interface Sound with maybe the method makeSound.
Class Dog, Cat and Duck are going to implement, they did a contract, this means that you are "usually" obliged to implement the method makeSound in Dog, Cat, Duck.  and so Dog will implement this makeSound method, focussing on barking, and so on!  Is not that fantastic? You will never forget you have to furnish this functionality because you did a contract.
The advantages of this contract are a lot, the most evident is that in the future if the Cat will emit another sound, because you are in a zoo and you have a special cat,  you will need to make minimal changes. This is the gold principle "encapsulate what varies", you are going to have less problem to make change at the code.

This is just the surface because interface is the butt and bread of something called Design Patterns, that means the expert guys noticed there are some recurring problems in programming and offered and collected some genial solutions that you can apply when a certain use case will show up

So you can use things as the strategy pattern https://www.tutorialspoint.com/design_pattern/strategy_pattern.htm for instance, and you will have your code much more decoupled, testable, closed for modification but open for extensions, will be more easy to use advanced concept as dependency injection, and you will implement new features in a much more "clean" way, less bug prone.

Another huge advantage is that interfaces can be tested really easy, also, they can be used to move information between classes with the so called callbacks, and much more.



 
Junilu Lacar
Sheriff
Posts: 15519
263
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Giovanni Montano wrote:
Another huge advantage is that interfaces can be tested really easy, also, they can be used to move information between classes with the so called callbacks, and much more.


This seems like a dubious claim. Would you care to explain how interfaces are inherently more testable than implementations? It's also not clear to me how interfaces would be more capable of moving "information between classes with the so called callbacks" than plain old classes.
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic