wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interfaces " Watch "interfaces " New topic
Author

interfaces

Tyler Jordan
Ranch Hand

Joined: Nov 17, 2003
Posts: 70
Could someone help explain a practical use of interfaces. I understand from reading "Head First Java" that they allow a program to implement methods from other inheritance trees. I also use subclasses and abstract classes in many programs, they are very useful. However, if there is no code in a method of an interface, how am I buying any additional functionality by implementing that interface? The bonus of inheritance is that the code is put in a single place and it can be used by classes that need it. How does it help if no actual code is in that class? A class (interface) without code can only act as an abstraction level, right? Can I get an example to help me understand how interfaces allow me to implement a useful function?
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Hi Tyler,
Interfaces are very useful because we can achieve the polymorphism effect by using them. If you don't have the idea of polymorphism then try to explore that first.
Tyler Jordan
Ranch Hand

Joined: Nov 17, 2003
Posts: 70
I do understand polymorphism and I create classes that use inheritance. I've used abstract classes and have exentended them to subclasses I have created, but I haven't yet seen a useful interface example. I'm sure they are very useful and thats why I want to understand, but I haven't seen why yet. Interfaces I have seen so far provide no functionality to my classes. How do they provide functionality?
Tobias Hess
Ranch Hand

Joined: Apr 06, 2004
Posts: 55
Interfaces I have seen so far provide no functionality to my classes. How do they provide functionality?
They don't. All they do is to make a statement about what messages can be send to objects of a class that implements that interface.
Here's a example:

Why do you want that? Well, it allows you to write code that talks to all objects of classes that implemet Stringable, without knowing what specific type you're dealing with.

This code works even with objects of classes that are implemented _after_ do() itself. Also, if you would do this with abstract classes, what kind of implementation of toString() do you want to put in it? You just don't know anything about the derived classes, and you don't have to with interfaces.
Let me suggest a book that provides more detailed explanations and comes for free, it is called Thinking in Java.
HTH, Tobias
[ April 16, 2004: Message edited by: Tobias Hess ]
[ April 16, 2004: Message edited by: Tobias Hess ]
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
HI
I found a good practical example for the use of interfaces in an article in JavaWorld. I felt it was good, the link is given below,
JavaWorld
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: interfaces
 
Similar Threads
Use of Interface
When to use interface in an application ?
multiple inheritance
Please help me to understand interfaces
Multiple Inheritence