File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Multiple Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multiple Interfaces" Watch "Multiple Interfaces" New topic
Author

Multiple Interfaces

Bill Boyle
Greenhorn

Joined: Aug 10, 2003
Posts: 12
I believe a class can only extend one class, but is it possible for a class to implement multiple interfaces. If so, how is this done. Also, can a class extend another class and implement an interface at the same time.
Thank you
Bill
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy
Yep, a class can extend one class, and -- at the same time -- implement one or more interfaces. For example, class Dog can extend Animal, but implement Pet, and Serializable:
class Animal {
// animal methods
}
interface Pet {
void beFriendly();
}
class Dog extends Animal implements Pet, Serializable {
void beFriendly() {
// tail-wagging code goes here
}
}
What this really says is:
A Dog is a type of Animal.
Inheritance defines what a Dog really IS.
Because of inheritance ('extends'), the Dog inherits things from class Animal.
But... a Dog is capable of playing other roles (wearing other hats).
So a Dog can also play the role of a PET, and do PET behaviors.
A Dog can also play the role of "something that can be Serialized" (which means saved/flattened/persisted)
But some *other* animal might not be appropriate as a Pet. For example, you might say:
class Hippo extends Animal implements Serializable {
}
This says that Hippo is a type of Animal, and is also "something that can be Serialized", but Hippo is not a Pet, and thus has no Pet behaviors.
The cool thing about interfaces is that they can cover things in different inheritance trees. So you can have other things in Java that are Serializable, for example:
class Book implements Serializable { }
This means class Book doesn't extend anything (except, implicitly, class Object), but it can also be serialized.
Or, you could do...
class RobotDog extends Robot implements Pet {
void beFriendly() { }
}
Now you have a class that is part of a different, non-Animal inheritance tree, but yet still gets to be a Pet. This is one of the coolest things in Java, because now you can treat Animals and certain non-Animals as Pets, polymorphically. (Out of scope to explain polymorphism here
cheers,
Kathy Sierra

are you wickedly smart?
"If you're a developer and you want to learn Java, dive in... Head First."
--Scott McNealy
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Originally posted by Bill Boyle:
I believe a class can only extend one class, but is it possible for a class to implement multiple interfaces. If so, how is this done. Also, can a class extend another class and implement an interface at the same time.
Thank you
Bill

You are correct in that a class may only extend one class. Classes may also implement multiple interfaces, either directly or via inheritence. A class may also extend another class in addition to implementing multiple interfaces.
For example:
public class MyClass extends MyBase implements MyInterface, Serializable {
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Although I see now Kathy has already answered you, and much better to boot. That'll teach me to wait so long before pressing submit.
Bill Boyle
Greenhorn

Joined: Aug 10, 2003
Posts: 12
Thanks Kathy and Jason.
It makes sense now.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Originally posted by Jason Menard:
Although I see now Kathy has already answered you, and much better to boot. That'll teach me to wait so long before pressing submit.

Yeah, well, he just happened to ask about one of my favorite topics. Trust me on this, if someone asks about bit-shifting, it's all yours I would most certainly make it worse.
cheers
-Kathy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Interfaces