aspose file tools*
The moose likes Cattle Drive and the fly likes Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Interfaces" Watch "Interfaces" New topic
Author

Interfaces

jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
This may be a completely silly question, and once I get some response or whatever, feel free to delete this DjD, but are interfaces there to force you to use certain methods? In other words, most interfaces don't have any real coding in them at all, so are they there to say "HEY! You forgot to add this!"
Thanks!
Jason
Amber Woods
Ranch Hand

Joined: Mar 28, 2001
Posts: 111
I am not the greatest at explaining sometimes, so here is a link from suns tutorial pages: http://java.sun.com/docs/books/tutorial/java/interpack/interfaceDef.html
Gives a better explanation than I could


"Happiness is a way to Travel, <b>Not</b> a Destination" -- Unknown
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Thanks Amber, I've been going over the tutorials today myself and was getting up the interfaces part anyways, so was nice to browse through, especially the last part about using them as parameters and the trouble with adding to them. I pretty much understand the concept and how to use them, just wanted to make sure I understand why they're methods were always empty.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
To tie in the previous conversations about sorting and casting...
Originally posted by jason adam:
<snip>...especially the last part about using them as parameters

I think you've hit on the the somewhat elusive thing that makes interfaces cool -- you can cast an object of a class that implements a particular interface to a reference of that interface (and pass it as a parameter to a method expecting the interface).
It's a sort of "sideways" cast (to borrow Pauline's terminology) -- because when you cast to an interface you're casting outside the inheritance tree. Unrelated classes can implement the same interface if necessary.
I think all the source-code and API digging I did during the SortNames discussions really improved my understanding of interfaces.
If you're looking for some really clever examples of how interfaces can be used, I'd highly recommend figuring out how both the sort methods on Collections work and how the two interfaces that get used pull it all together.
(By "two interfaces" I mean the one you override as part of the assignment and the one you rely on "as is" for the first sort.)
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by jason adam:
This may be a completely silly question ...

The purpose of an interface
Definitely NOT a silly question!

Interfaces are in the language to allow a class to say it behaves like some other class and can be used in place of the other class.

1. They are Java's version of multiple inheritance
2. They are used for call-backs
3. They declare methods that any object can implement & thereby become extensible.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Originally posted by Michael Matola:
...you can cast an object of a class that implements a particular interface to a reference of that interface (and pass it as a parameter to a method expecting the interface).

unhh, what? Could you give us an example of this, please?
Thanks! Pauline

[This message has been edited by Pauline McNamara (edited July 11, 2001).]
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2

(1) Since Humans, Octopuses, Birds, and Superman are Animals you can put instances of them in an Animal array. Same thing with Trucks and Planes -- they can go into an array of Vehicles.
(2) Since Birds, Planes, and Superman implement CapableOfFlight, you can also put them into a CapableOfFlight array. It's not sideways in the sense of casting something from one inheritance hierarchy to another, but in the sense that objects from different hierarchies can implement a given interface and be "cast" to that type.
(3) You can call the canFly() method on these directly.
(4) & (5) Or you can have a method that accepts any object implementing CapableOfFlight, and call canFly() within that method. This is what I was talking about in my original post.
Marilyn -- would you call what I'm doing in the method printOnlyThoseCapableOfFlight a kind of call-back? It seems that the point of that method (or the point of having it take only CapableOfFlight parameters) is to call a CapableOfFlight-specific method on the object passed in.
I changed the size of the code
[This message has been edited by Johannes de Jong (edited July 11, 2001).]
I modified the code slightly to keep this from blowing off the page.--Carl
[This message has been edited by Carl Trusiak (edited July 18, 2001).]
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Fun example Michael. So, just to check that I'm getting this...
the CapableOfFlight array is the interface reference that you mentioned earlier,
and when you put those objects implementing CapableOfFlight into that array, that's what you meant by "casting" them...
right?
Thanks,
Pauline
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

check out http://www.javaranch.com/CodeBarn/shapes.jsp
I have some issues with the flight example here. Mostly of a choice-of-identifier angle.
If your class implements CapableOfFlight, it would seem that that class is then capable of flight, which is not always so.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Is the way you create an object of type Superman a standard pattern or idiom in Java for creating an object of a class that you only want one object of? I was about to say instaniated, but you instantiate the object with Superman superman = Superman.heySuperman(), but there is no new operator.
Just curious when you would commonly use this so that I can stick in the back of my head for usage at some later date.
Thanks!
Jason
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Isn't Superman "instantiated" with:
private static final Superman theOneAndOnly = new Superman() ;
and we just "get to him" using:
Superman.heySuperman( );
?
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I think you are right, the object is created in the Superman class itself with

and we pass the reference of this object to superman by

I'm still curious if this is a common Java practice. Never written a class that I want only one object from that class to exist.
Of course, I think this is diverging from the original discussion on interfaces some

[This message has been edited by jason adam (edited July 12, 2001).]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by jason adam:
I'm still curious if this is a common Java practice. Never written a class that I want only one object from that class to exist.

Using the singleton pattern is common. This is where you want to limit the number of objects of a particular class to one.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Originally posted by Paul Wheaton:
I have some issues with the flight example here. Mostly of a choice-of-identifier angle.

Paul, I'd be glad to read any further criticism. Although my posts often have an assertive "this is how things work" kind of tone, it's obvious I'm still learning the ropes with Java and OO. Writing these kinds of things tests my knowledge. I know that sometimes I'm going to be wrong. I wouldn't be posting code in public forums if I couldn't take the heat.
I'm not really satisfied with the names either. I like the -ble suffix names that many Sun-delivered interfaces have and tried to go with something along those lines.
Thinking up short, useful examples can be difficult. I wish I had been able to think of more clever things to do than just print strings.
If your class implements CapableOfFlight, it would seem that that class is then capable of flight, which is not always so.

Are you thinking of emus and ostriches and the like -- birds that can't fly? Sure I know that.

This is close to what I was trying to get at.
This conversation on interfaces was an outgrowth of several different conversations on collections, sorting, and casting in the SortNames assignment.
I spent quite a bit of time reading the API and source code for the relevant classes for that assignment. I was deeply impressed in particular with the Comparable interface (and that other interface relevant to SortNames) and how they get called in the bowels of Array.mergesort(). The implications of Comparable are profound -- for any class you ever write in Java if you ever think it might be useful to sort objects of that class into some "natural order," all you have to do is declare that the class implements the Comparable interface and write a compareTo() method to decide which object in a pair comes first.
This is the kind of thing I had in mind when I made my original cryptic comment about casting to an interface, etc.
But I didn't want to launch into a full-fledged grokfest about how clever I think Comparable and that other interface are, for fear that I'd give away too much of the SortNames assignment.
*****
Now, regarding the CodeBarn Shapes example.
If I write a class such as this.

And add the following lines to the code that populates the static shapes array in ShowShapes.
// can be fun at times
new Text( 247 , 225 , "can be fun at times" , Color.orange ) ,
Would you still think that Shapes is a particularly good name for that interface?
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Pauline and Jason --
Sorry for dropping out of the conversation. I've been very busy this week. The biggest project I've developed so far at work had to be ready to move into QA for testing.
It sounds like you understood the point I was trying to make about interfaces.
But found the singleton more interesting to talk about...
(I knew I should've taken that out -- what a distractor!)
Early on when I started to learn OO I was constantly seeing reference to this book called Design Patterns (the four authors are commonly referred to as the "Gang of Four") and just had to get a copy. (If you search the web, JavaRanch, or Amzaon.com, etc. for this book, you'll find more than you ever wanted to know about it.) In 25 words or less -- the book desribes a number of very generalized OO solutions to common design problems. This singleton thing -- how to design a class so that there can only ever be a single instance of it -- is from that book (the examples in the book are in C++ and Smalltalk).
I understand only a handful of the design patterns. Two of them from the book (that I know of) are actually built into Java -- Iterator and Observer/Observable. When I wrote something about Iterators in one of the SortNames threads last week, I later went back and read the chapter on the Iterator pattern in Design Patterns. That's why this stuff has been on the brain.
When I wrote up the interface example, I just thought it might be fun to make my Superman class a singleton.
Now anybody want to talk about that ugly StringBuffer thing I wrote?...
[This message has been edited by Michael Matola (edited July 14, 2001).]

It's UGLY -- Marilyn
[This message has been edited by Marilyn deQueiroz (edited July 14, 2001).]
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Thanks y'all for all of the responses, I definitely see that interfaces are more than just a way to force a class to use certain methods. Prime example is creating an inner class in a applet that implements the ActionListener interface. I used to do it just because that's how I had seen it done, using the tempelate but not understanding the concepts behind it. Now when I look at examples of code, such as applets, I understand a lot more what is going on and don't feel like a drone.
Since I have had absolutely no training or education in formal OO concepts or design, I haven't a clue as to the pattern things y'all have mentioned. I think perhaps now I should start focusing on reading more about that and not worry so much about learning the high level Java stuff (servlets, JDBC, etc).
Jason
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Jason, I completely understand what you mean about writing something some way just because that's how you've seen it done versus really understanding what's going on.
For certain things, you may not need to know the gory details. You just follow what the API tells you to do and it works.
I think the thing to remember when you're implementing a Java-delivered interface is that some code -- somewhere -- is going to call the method(s) you're overriding.
It just became a fun exercise for me to track this down for the interface(s) used in SortNames.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Also, I don't have any formal training in any of this either. I just read a lot.
I was intimidated by OO when I first started learning Java. There seems to be so much baggage associated with OO. What it is, what it isn't, how it should or shouldn't be done, etc.
I was intimidated almost to the point of being fearful to write Java code because some mythical OO guru might proclaim it badly designed.
Two things helped me get over this -- the book Refactoring: Improving the Design of Existing Code by Martin Fowler and participating in the Cattle Drive.
Refactoring shows techniques for making safe changes to existing code to improve the design -- for turning non-00 code into OO code and for making OO code better. The point of the book is more "how to get there from here" -- but what I was able to get out of it was more "what here looks like" -- that is, some examples of good OO design. (The examples in this book are in Java.)
But on the other hand, I've done the first couple of Servlet assignments and they're a lot of fun too!
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Since I have had absolutely no training or education in formal OO concepts or design, I haven't a clue as to the pattern things y'all have mentioned. I think perhaps now I should start focusing on reading more about that...

Find the book Design Patterns, "The most popular computer science book of all time," here or elsewhere. It's very useful and not just for Java.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Getting back to interfaces.
You can do a search for interface in other forums. One good explanation is here
Another good one and this one might also be useful.

[This message has been edited by Marilyn deQueiroz (edited July 15, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Interfaces