File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes what is the use of Interfaces in the real world example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is the use of Interfaces in the real world example" Watch "what is the use of Interfaces in the real world example" New topic

what is the use of Interfaces in the real world example

murali kankanala
Ranch Hand

Joined: Nov 15, 2004
Posts: 110
Hi folks,

Can you please explain me what is the use of Interface in real world example.
Simply i can write a class give it to the client to access my methods, but what is use of implementing an interface.

thanks in advance
John de Michele

Joined: Mar 09, 2009
Posts: 600
Interfaces allow a great deal of freedom, since they specify behavior vs. specifying an implementation. In some model-based testing that I am working on, Nodes use an interface called Driver to move through their testing:

Driver specifies just two methods: registerState, which stores a State object for testing, and run, which executes the implementing class's behavior. Right now, I have some classes that use Selenium for testing that implement Driver. If I decide in the future to use a different web driver, say Canoo Webtest, all I have to do is create some classes that handle Canoo Webtest, and have them implement Driver. Then, I can plug them into a Node object, and can go on testing without doing a bunch of recoding.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42950
You may also want to work through the relevant Java Tutorial chapter:
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11499

Say you and I both write a classes that do something cool, albiet slightly differently - they would be so cool that every software company out there would want to use one or the other in their application, and pay one uf us LOTS of money.

You write your class, and give everyone the API for your class. They write references to the name of your class, using your methods:

MuraliClass mc = new MuraliClass(int a, int b, string c);


I write my class, and have my class implement an interface. I then give all of MY customers the Interface (the name of the interface and the method signiatures), and they code everything against it"

FredInterface fi = new FredInterface(int a, string b, string c);

now, a year goes by. We both work on improving our classes. We both discover that we can write something BETTER, that is faster, more efficient, and uses less memory. You write a new class, and call it "MuraliClassTwo". I write a new class, and call it FredClassTwo. Your customer has to re-write all their code and change all their references to MuraliClassTwo. My customers DON'T HAVE TO TOUCH THEIR CODE, because they are still getting a FredInterface, even though the underlying class has changed.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: what is the use of Interfaces in the real world example