File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes why interfaces needed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why interfaces needed?" Watch "why interfaces needed?" New topic
Author

why interfaces needed?

kiran nyala
Ranch Hand

Joined: Sep 21, 2009
Posts: 47

why interfaces introduced in java?
what is the structure of interface? Explain exact usage of interface
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42592
    
  65
Start here here: http://java.sun.com/docs/books/tutorial/java/index.html, particularly the "Interfaces and Inheritance" chapter.


Ping & DNS - my free Android networking tools app
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

To summarize in a single sentence,

I wrote:An interface provides a standard for implementation.


It's very very powerful once you understand java...


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

Joined: Feb 12, 2010
Posts: 52
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?


Serap Elbeyoglu
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
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

Joined: Feb 12, 2010
Posts: 52
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

Joined: Oct 13, 2005
Posts: 39773
    
  28
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

Joined: Feb 12, 2010
Posts: 52
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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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.


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

Joined: Feb 12, 2010
Posts: 52
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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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

Joined: Feb 12, 2010
Posts: 52
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why interfaces needed?