aspose file tools*
The moose likes Beginning Java and the fly likes what is the need of interface? 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 need of interface?" Watch "what is the need of interface?" New topic
Author

what is the need of interface?

rahul mehra
Ranch Hand

Joined: Aug 20, 2007
Posts: 33
why is interface needed in java.... if we say that any class which implememnts a particular interface has the flexibility to define its method in its own way..then we might as well as declare a class with the required method....


why do we need to define an interface.... ???


can anybody give me the basic idea.....


[ November 16, 2007: Message edited by: Bear Bibeault ]

Thanks
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Look at the doc for List. This is an interface, so I can write something like:

I never need to know exactly what kind of List somebody calls me with - a LinkedList, Arraylist, etc. You can write a whole new class that implements the List interface and pass it to my method and I'll work just fine.

The next question is probably: How is interface better than abstract class? AbstractList happens to exist in the library so there must be a place for abstract classes here, too. Try the search widget at the top of the page for "asbtract vs interface" in the FAQs.

Does that answer the right question? Raise some new ones?
[ November 16, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Venkat Sadasivam
Ranch Hand

Joined: May 10, 2008
Posts: 139
Though it is old question, I would like to give some good answer for this question.

Sun introduced Interface concept to avoid diamond inheritance problem in C++.


“Any fool can write code that a computer can understand. Good programmers write code that humans can understand. ”<br>
-Martin Fowler
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Originally posted by rahul mehra:
why is interface needed in java.... if we say that any class which implememnts a particular interface has the flexibility to define its method in its own way..then we might as well as declare a class with the required method....


why do we need to define an interface.... ???


can anybody give me the basic idea.....




In addition to the use within the Java language itself that Stan talks about, interfaces are very useful in another way. A classic example of the power of interfaces is the JDBC (Java Database Connectivity) API. If you look at the API docs for the java.sql package, you will see there are a lot of interfaces defined for which there are no actual implementing classes. It is left to to the database vendor to actually implement the class. In fact, Java itself does not have implementations of these interfaces.

This allows me to write code based on the interface, say a Statement and a ResultSet, that works regardless of the underlying database.



That code is programmed entirely against the interfaces in JDBC. At runtime, I may use the MySQL JDBC JAR file in my classpath which has the ResultSet and Statement implementations for the MySQL database; Or I might use the Apache Derby JAR, or the Hypersonic SQL DB JAR, or Oracle JDBC JAR, etc. It doesn't matter; my code runs the same because I programmed to the interface. This makes the code far more flexible. I can switch the underlying database without having to make any code changes. This prevents me from getting locked into a specific database vendor. (At my old job, when our database vendor was going to significantly raise licensing costs, we simply switched database vendors with no code changes. We could do such because of the JDBC API.) The JDBC API also provides more general flexibility. For example, in my project at work, our test code runs against a very light weight (and free) in memory test database; where as the same code, when in production, runs against a full scale database.

That is very powerful and useful.

p.s. you can read about the Diamond of Death Venkat Sadasivam mentioned at Wikipedia.
[ August 16, 2008: Message edited by: Mark Vedder ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
With other words, interfaces allow the use of runtime polymorphism with static typing and without implementation inheritance.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the need of interface?