Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll look at a couple other ways interfaces help us to write good code!
Code to Copy
// Create Objects
ShopKeeper shopKeeper = new ShopKeeper("Larry");
Teacup teacup = new Teacup();
String treehouse = "Treehouse";
// Loop through Objects
Object[] objects = {shopKeeper, teacup, treehouse};
for (Object object : objects) {
if (object instanceof Chattable) {
String response = ((Chattable) object).chat();
System.out.println(response);
}
}
// Loop through Chattables
Chattable[] chattables = {shopKeeper, teacup};
for (Chattable chattable : chattables) {
String response = chattable.chat();
System.out.println(response);
}
Related Links
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
We just finished implementing
our Chattable interface.
0:00
Now to see how this interface helps us,
let's head over to Main.java, and
0:03
look at a couple concrete examples.
0:07
Let's start by deleting the comment.
0:10
And then let's copy and paste in
some code from the teacher's notes.
0:14
And take a second to
get the spacing right.
0:19
Great, let's look through this code,
starting from the top.
0:25
The first thing we do is
create a few objects.
0:29
We've got a ShopKeeper named Larry, a
TeaCup, and a String that says treehouse.
0:32
Then we create an object array containing
all of the objects we just created.
0:38
Next, we look through the array and
0:43
then side if an object isn't
instance of Chattable,
0:46
then we cast that object to
a Chattable and call the chat method.
0:51
Before we had our interface,
0:58
we didn't have a good way to find
out if an object had a chat method.
1:00
But now with the interface,
we can just use instanceof Chattable,
1:04
and we'll know right away
if it has a chat method.
1:10
Another benefit of interfaces
is that we can use them
1:13
as a type like in this charitable array.
1:16
It doesn't matter that a shopKeeper and
the teacup are different things.
1:20
All that matters is that
they implement chattable.
1:24
So when we are looping through
our ray of chattables,
1:27
we can just call the chap method
without casting anything.
1:31
And if we run the app,
we should see the same output twice.
1:36
We should hear from the shopKeeper in
the teacup up here and down here as well.
1:40
Let's run it.
1:45
And that's exactly what we get.
1:48
A good way to think about
interfaces is as job postings for
1:51
objects, it's a list of actions that
an object has to do to get the job.
1:54
For example, if we have a Pilot interface
with a flyPlane method, then any
2:00
object implementing that flyPlane method
will be able to get the job of Pilot.
2:05
This gives us a ton of flexibility
around who can be pilots.
2:10
We could make a human pilot, a dog pilot,
we could even make a toaster pilot.
2:14
Or do something more abstract, like make
the feeling of happiness into a pilot.
2:19
The point is, having the pilot's
functionality in an interface
2:23
instead of a class gives us a lot more
flexibility when writing our code.
2:27
This is actually a well known
programming principle called Composition
2:33
over Inheritance.
2:36
When you are creating an application,
rather than attaching functionality to
2:38
a class and requiring it to be inherited,
you should put that functionality
2:42
into an interface and then create classes
composing of the required interfaces.
2:46
A good example of this would be to
look back at the vehicle example
2:52
from the last course.
2:55
Instead of ground and air vehicles, we can
just have drivable and flyable interfaces.
2:57
And instead of the vehicle class,
3:03
we can just use an interface to tell us
how many passengers something can have.
3:05
Now, all we need to do to create our
classes is combine these interfaces.
3:10
[SOUND] We can even upgrade our
plane to be drivable and flyable,
3:14
because let's face it, most planes
have to travel on the ground too.
3:18
Getting back to our code,
3:23
let's look at one more example of where we
should favor composition over inheritance.
3:25
Back in the ShopKeeper class,
3:30
we see a similar problem to what
we had with the chat method.
3:32
This sellGoods method is currently tied to
the ShopKeeper class which is a person.
3:37
So if we wanted to use the same sellGoods
method to create a vending machine,
3:44
we'd be out of luck.
3:48
To fix this, let's create a new
interface named seller and
3:50
give it the sellGoods method.
3:54
Let's go New > Java Class, name it Seller,
3:57
and make it an Interface.
4:02
And inside,
let's declare this the sellGoods method.
4:07
Void, sellGoods and
it takes no parameters.
4:10
Finally to finish it out, let's head
back to the ShopKeeper class and have it
4:14
implement our cellar interface by adding
implements and then the seller interface.
4:19
Which means we'll need to
make this method public, And
4:25
we'll want to add the override annotation.
4:30
Perfect, interfaces are an important
component of many Java programs,
4:34
leading to much cleaner code.
4:39
And being able to separate out what
something does versus how it does it
4:41
gives us a lot more flexibility
with writing our programs.
4:46
Until next time.
4:50
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up