aspose file tools*
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
Author

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
Rancher

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
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
You may also want to work through the relevant Java Tutorial chapter: http://java.sun.com/docs/books/tutorial/java/IandI/index.html


Ping & DNS - my free Android networking tools app
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

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);

etc.

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: what is the use of Interfaces in the real world example