File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Interfaces (basically speaking) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces (basically speaking)" Watch "Interfaces (basically speaking)" New topic

Interfaces (basically speaking)

Jimmy Bonds
Ranch Hand

Joined: Jun 05, 2001
Posts: 38
Hi All-
Am I correct in thinking (basically speaking) that interfaces are just a bunch of abstract classes that are just used for multiple inheritance? So you could have a class that implements 10 or so interfaces as long as the class provides definitions for all members of the interfaces?
Is it this simple? Or is there something complex to it?
yah, yah I'm a newbie
rajesh purohit
Ranch Hand

Joined: May 02, 2001
Posts: 33
generally interfaces are used for implementing multiple inheritance. you can implement 10,20 or more interfaces in ur class
But interfaces classes are not abstract classes...
bye for now
Rajesh Purohit
visit my site

Bye for Now<P>Rajesh Purohit<BR>INDIA
karl koch
Ranch Hand

Joined: May 25, 2001
Posts: 388
interfaces are here to assure methods are present in the implementing class (eg. ActionListener interface needs public void actionPerformed(ActionEvent ae) ).
you cannot inherit implementation through an interface. so it's up to you to code the methods from the interface.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
It is that simple. But of course that does not give you the true feeling for the POWER of interfaces.
Lets say that I want to create a class or a method that takes a parameter. Only I don't know WHAT exactly the thing that is going to be passed is going to BE. You can't put a variable in the method definition
public void myMethod(IDontKnowWhat x){ //do stuff};
However you do know that in the do stuff area that you need the thing that is passed to be able to have specific behaviors. For instance, let's say that whatever is passed has to be able to execute a play() method.
Well if you have an interface that includes a play() method (let's call it BabyStuff) then you know that you will be able to handle any class that implements BabyStuff. Now you can code your method using the interface name as the "variable parameter" and be safe.
public void myMethod(BabyStuff x){ //do stuff that includes play()};
The same theory holds true for Fields (variables). You can create a variable that can hold a variety of objects by
BabyStuff b = (an instance of any class that implements BabyStuff);
Now you can use the "b" variable freely, even though you do not know what EXACT class that the object it refers to is. Of course while it is referenced through the "b" variable you can only expect it to do BabyStuff things even though as an instance of some other class it can probably do much more, but it is limited by the type of the variable that is holding it. On the other hand you have gained the ability to be able to hold a variety of different class objects this way, and be able to manipulate them in a consistant manner.
It is for these reasons that there is a basic principle of Object Oriented design that says:

Program to an interface, not an implementation

("Design Patterns" - Gamma, Helm, Johnson & Vlissides)

[This message has been edited by Cindy Glass (edited June 21, 2001).]

"JavaRanch, where the deer and the Certified play" - David O'Meara
Jimmy Bonds
Ranch Hand

Joined: Jun 05, 2001
Posts: 38
Thanks Cindy for the great explanation
Rajesh and Karl Thanks to you guys too!
yah, yah I'm a newbie
I agree. Here's the link:
subject: Interfaces (basically speaking)
jQuery in Action, 3rd edition