permaculture playing cards*
The moose likes Beginning Java and the fly likes What good are interfaces? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What good are interfaces?" Watch "What good are interfaces?" New topic

What good are interfaces?

John McGahee

Joined: Jan 10, 2003
Posts: 5
If all the methods in an interface are abstract what real purpose do they serve. I have a sneaking suspision that you can define an interface extending a defined class and that way you can use the properties/methods of that class
something like
class usefulUtility{
void methodA(){}
interface useless extends UsefulUtility{
abstract void methodB(){
So essentially even though you cannot manipulate the class you can use its methods when you implement the interface. Coz if thats not the case, I really don't understand why you would use interfaces???
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You cannot extend an interface using a class. An interface is always a pure abstract class.
Interfaces are Java's way of handling multiple inheritance. Interfaces are widely used throughout the Java API. The AWT event model is built around them as is JDBC.
Let's say that you write a Printer class that can read in another class and print out a meaningful representation of the class specific to printing. (This would be different than the toString() representation.) Each class would be responsible for formatting itself since there is no way for the Printer class to know how each and every class should be formatted to print. You decide to add a method called toPrint() to each of your classes. But how does the Printer class know that each class that is passed to it has a toPrint() method? That is where interfaces come in.
Look at this interface:

Now any class that implements the IPrintable interface must have a toPrint() method. The Printer class can now be coded with this:

Only classes that implement the IPrintable interface can be passed to the print method.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
John McGahee

Joined: Jan 10, 2003
Posts: 5
Excellent explanation Thomas. Let me be a pain in the a** now, how is it that we can manipulate List objects, where List is just an interface? It has methods and everything that can be used.??
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
When your software uses a List, it never actually gets an object of type List, just one of the many classes which implement it. The methods of the List interface are handled by the underlying object.
Try it: put System.out.println("real type of list object is '" + list.getClass() + "'"); in your code and see what you get!

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
And you might want to take a look at the "How my Dog learned Polymorphism" article in The JavaRanch Campfire Stories.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Take a look at the Collection articles I wrote for the JavaRanch newsletter (Starting with the June issue, I believe).
The secret is that since List is an interface, we never actually have an instantiated List. Instead we can do something like this:
List myList = new ArrayList();
myList looks like a List but in reality it is an ArrayList. You can change myList to an ArrayList if you like:
ArrayList myArrayList = (ArrayList)myList; // casting required
and back again:
myList = myArrayList; // no casting is required since we are going up the hierarachy
The secret is that no matter what we use to hold the object it is always an ArrayList because that is what we instantiated. We can cast it to anything up the hierarchy from what it really is without any problem:
Object myObject = List;
but it is still in reality an ArrayList. The pointer type determines what method the compiler will let us run. The actual type determines what gets executed at runtime.
I agree. Here's the link:
subject: What good are interfaces?
Similar Threads
Difference Between Abstract Class and Interface
Abstract vs. Interfaces
Class within Interface
difference between interface and abstract classes