• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing interfaces

 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to understand the benefit of defining interfaces that need to be completed by the classes that implement them. The interface method contains no body, and must instead be completed in a method of the implementing class.
I understand the implications imposed by interfaces, such as the rules for object reference casting, but what I'd like to know is the actual benefit of having an interface at all. As opposed, that is, to simply defining the method from scratch in the class that uses it.
Is it purely to keep similar functionality standardised and in the same place?
I'm sure there must be more to it than this...
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As an example, look at the java.lang.Runnable interface and how it's used with the java.lang.Thread class.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AS Mark said the Runnable interface is a great example. Since it exists and we know that SOMEDAY you are going to create a new class that implements Runnable, we can use that knowledge, without even knowing the name of your SOMEDAY class, to create a Thread class that takes Runnable as an input.
Of course it can not take the name of you SOMEDAY class as an input - you haven't even named it yet. But still it is that class that we will eventually want to operate on. But knowing that you are going to implement Runnable allows us to create Thread with a method start() that invokes the method run() from Runnable.
This whole framework is set up in advance, and can only exist because of the existance of interfaces.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was pretty confused by interfaces at first, but a friend of mine explained it really well. So, I'll try and regurgitate (sp?) his explanation:
Basically an interface is a contract. When you declare a class:
<CODE>
public class Peanut implements Smashable
{
//code for being a peanut
//plus code to implement methods declared in Smashable, say ...
//smash(), grind(), and mangle()
}
</CODE>
You are promising that all instances of class Peanut will be able to handle the methods contained in Smashable.
Now imagine that we have a bunch of classes implementing Smashable (eg. Walnut, Hazenut, CuteFuzzyKitten). All these classes promise to be able to handle the methods declared in Smashable.
We can now create a method like this:
<CODE>
public void Pulper ( Smashable[] stuffToPulp )
{
//Code that calls smash, grind and mangle on Smashable objects.
}
</CODE>
The advantage here is that you can put peanuts, walnuts and CuteFuzzyKittens all together in that same array and treat them as objects of the same type (type = Smashable).
I'm sure there are probably other uses for interfaces. They basically exist to make up for the fact that, unlike C++, java doesn't support multiple inheritance. But I've never worked in C so I dunno all the implications of this.
I imagine that in large projects interfaces become very useful for isolating your classes from the clients that use them. But I'm not really working at that level yet so 'tis pure conjecture.
Hope that helps,
Joe

[This message has been edited by Joe Von (edited March 07, 2001).]
[This message has been edited by Joe Von (edited March 07, 2001).]
 
Mark Howard
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for these replies - they certainly have clarified the concept of interfaces.
Mark
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
CuteFuzzyKittens are not Smashable!!
Also, here is an article that may help you with interfaces: http://www.artima.com/designtechniques/interfaces.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic