Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why interfaces needed?

 
kiran nyala
Ranch Hand
Posts: 52
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why interfaces introduced in java?
what is the structure of interface? Explain exact usage of interface
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start here here: http://java.sun.com/docs/books/tutorial/java/index.html, particularly the "Interfaces and Inheritance" chapter.
 
salvin francis
Bartender
Posts: 1263
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To summarize in a single sentence,

I wrote:An interface provides a standard for implementation.


It's very very powerful once you understand java...
 
Serap Elbeyoglu
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to everyone,

I read early posts. Addition to this I am reading Sun's tutorial. But I find hard to understand interface.

Can we say interface provides interaction between Java developers who are working on the same project? Is it right?

How do they manage this? Only with implements keyword?
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might provide interaction between two people working on the same project. But it provides much more. If you say "implements List" then you know there are 25 methods which are available, and what they do, but you don't know how they are implemented.
 
Serap Elbeyoglu
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Campbell Ritchie,

Can you explain more? One of the function of implements is providing interaction between developers. I got this. What are the other functions?
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface indicates there is a consistent range of functionality. As I said, "implements List" means there will be 25 methods, which you will find in the List interface.
 
Serap Elbeyoglu
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Campbell,

I have made some research. I think I got some point of interfaces. It is not so difficult as I supposed.

I have opened a new topic about writing flexible code. Please visit it.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be a pain...

I would say interfaces are not NEEDED. You could write lots and lots of code and never NEED to use them.

but they sure do come in handy and can make life much easier.
 
Serap Elbeyoglu
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:



can make life much easier.


Hello Fred, can you tell us how can they make life much easier? Thank you.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You and I both work at the same software company. We need to buy third party software for controlling a robotic arm. We are competing for a huge bonus.

The third party I use provides an interface. They give me a list of methods their class will implement. I write me code to call those methods. I don't know, or care, how they work internally, I just know the method signature. So I write my code to call 'raiseArm(3)' or 'turnXAxis(15)'.

The day before my presentation to the board, my 3rd party vendor has a brand new release that is 10 times more efficient. Since their new code uses the same interface, I don't have to touch my code - I just drop in their new compiled classes.

Your third party gives you a list of methods. You write your code calling these explicit methods. They day before your presentation to the board, your vendor releases a new version that is 10 times more efficient. But all their method names changed from something like 'raiseArm()' to something like 'raiseArm2()'. For hundreds of methods.

Do you a) go in front of your board of directors and say "We're using the old version of the vendor software that is slow", or b) Spend all night re-writing your code to call the new methods, re-testing and validating every line?

Notice that since my code didn't change, I don't have to re-test it. You do. And in six months, when both vendors come out with their version 3.0 software, I can be sipping a cocktail on a beach spending my bonus money, while you have to work ANOTHER all-nighter to get your code working again.
 
Serap Elbeyoglu
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred, it is a great explanation. Thank you, it is so clear to understand the necessary for interfaces.

You and me will compete again and I will get the big bonus
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic