• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Interfaces

 
Bill Boyle
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kathy and Jason.
It makes sense now.
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic