aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Declaring an Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Declaring an Interface " Watch "Declaring an Interface " New topic
Author

Declaring an Interface

James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

So I read through this section of SCJP by Bates and Sierra. My first question is how can a method me abstract. I didn't think we could create objects from methods, but we created objects from classes.

Secondly what's the purpose of using interfaces? I used one called actionListener(I think) to keep track of where I clicked my mouse. But if there cannoot be any code in the interface methods then whats the use if the programmer has to create the code anyways.

I really do appreciate the exam watch parts throughout the book, they will be very benefical.
[ July 27, 2007: Message edited by: James Hambrick ]

Visit my blog! http://jameshambrick.com
Paul Napauleon
Greenhorn

Joined: Jul 20, 2007
Posts: 15
We use interfaces to keep the real implementation (of the concrete class) hidden for the software which uses the interface. Some software calls on (abstract) methods on an interface, without knowing how that interface is implemented. This principle is called "abstraction".

Methods which are defined abstract HAVE TO be implemented by a concrete class.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

So I have the example in the book


then I have it implemented
[code]
class Tire implements Bounceable{
public void bounce(){
................
}
public void setBouncefactor(int bf){
..................
}
}

then Bounceable hides the class Tire from the software. what software? you mean if the Tire class is reused on someone else's program. Why hide Tire from the other software? I mean when I used actionListener I had to find out that I could use getX() and getY() from somewhere(don't remember where). if you hide that then the programmer has to find it out somehow. I understand how to make it work, just don't understand why I would want to use it, ie: what are the benefits to hiding classes. Why not just make everything provate and use get and set methods?
[ July 27, 2007: Message edited by: James Hambrick ]
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

the interface only shows what methods can be used and not the details of what the method does. Like in actionListener I used mousePressed and inside it I used getX and getY. The code that made these methods work was not important, only that it worked when I needed it. I think I have talked myself into the answer. not sure if hiding the method and class details is for abstraction, or to hide the code that programmers made so that no one else can use it without them getting credit. So that noone can look through the code and learn from it and that no one can steal it.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

So then my next question is when should I make a class have an interface? any theories on that? When I think the class is useful enough to have one.
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Well a couple of things are handy to know....

An interface is not part of the "class hierarchy", so it can be implemented by a class without regard to the class hierarchy that you might be currently building.

You can also implement multiple interfaces where you can only extend one class.

An interface can be implemented by totally unrelated class hierarchies.

Sort of like a "mom" interface. She tells you, "you have to clean your room!", "you have to put your clothes in the laundry", "you have to wash the dog", " and "you have to do your homework".

Notice... she isn't telling you HOW to do it... she leaves the implementation up to you. SHE just tells you the points of the contract that you have to fulfill..... and, by golly you HAVE to implement those items.


------------------------
Bob
SCJP - 86% - June 11, 2009
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

You can also implement multiple interfaces where you can only extend one class


So this is kinda like multiple inheritance

An interface can be implemented by totally unrelated class hierarchies

Can't you do that with inheritance too???
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
not right in the middle of another inheritance chain and in addition to the inheritance chain.
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
You are all forgetting about polymorphism, the real reason why we have interfaces. AN interface is a contract of services, a guarantee that certain behavior will be present.

IF a have a DAO (Data Access object) that has a getCustomer() method, I don't care how the class that implements the interface that requires an implementation of getCustomer() works. I just know that it will
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I posted a detailed example under this related topic. I hope it helps.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
Consider Paul's rocket mass heater.
 
subject: Declaring an Interface