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 introduce interfaces and see what type of problems they can solve!
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
[MUSIC]
0:00
In the last course,
0:09
we saw how inheritance let's us use
existing classes to help build new ones.
0:10
Unfortunately, there are a couple problems
that can't be solved by inheritance.
0:15
Which means it's time to talk
about the birds and the bees.
0:20
You see, birds and
0:25
bees are both capable of flight,
meaning they both have a fly method.
0:26
However, since not all animals can fly,
0:32
these two fly methods are totally
separate, which isn't ideal.
0:35
What if we needed to create an array
containing only animals that can fly?
0:40
Right now, it would be pretty hard to do.
0:45
Even though birds and bees have different
implementations of the fly method,
0:48
we can still pull the fly method out
into an abstract class like this.
0:53
Then if we make the Bird and
0:59
Bee classes extend Flyable as well
as Animal, we'd be good to go.
1:01
Except of course that Java doesn't
allow for multiple inheritance.
1:07
To see why that is,
let's look at an example.
1:11
Let's say our Animal class
has a method called speak.
1:15
And extended from Animal, we have two
classes that override that method.
1:19
The Human class, which says Hello,
and the Horse class, which says Hay.
1:23
So far so good.
1:29
Now, let's say someone comes along and
1:31
makes a Centaur class by extending
from both the Human and Horse classes.
1:33
But for some reason,
they don't override the speak method.
1:39
This is called the diamond problem.
1:43
If we called the speak method on
a Centaur object, it's not clear
1:45
if that's supposed to be the Human
speak method or the Horse speak method.
1:50
There's several ways to
solve the diamond problem.
1:54
But for Java, the solution was to
get rid of multiple inheritance.
1:57
Instead, we have interfaces.
2:02
You can think of
an interface as pretty much
2:04
just a more restricted abstract class.
2:07
Let's go back to the birds and
the bees to see what I mean.
2:11
On the left, we have the code from before,
where we used multiple inheritance.
2:15
And on the right,
we're using an interface.
2:20
These two pieces of code
are exactly the same.
2:23
Let's take a closer at the differences
to see how to use an interface.
2:26
For starters, when creating an interface,
you start with the interface keyword.
2:31
Then, just like with the class,
you add the name and then the brackets.
2:36
Inside an interface,
there's really only two things you can do.
2:42
You can declare a constant, or
you can declare an abstract method.
2:46
Since we're limited to just constants and
abstract methods inside an interface, we
2:50
don't need to specify static final for our
variables, or abstract for our functions.
2:56
It will just be that way by default.
3:01
Once we've created the interface,
3:04
we associate it with a class by
using the implements keyword.
3:06
Then, since interface
methods are abstract,
3:10
we need to override each of
those methods in the class.
3:13
This is one of the big
advantages of interfaces.
3:17
Since they can't contain
any method implementations,
3:20
it's impossible to run
into the diamond problem.
3:23
Which means we should have no problem
implementing from more than one interface.
3:26
So if we wanted to make
our Bird class Flyable and
3:32
Singable, we'd just add a comma and
then the Singable interface.
3:35
Awesome, that covers the basics.
3:40
Let's take a short break, and
3:43
when we come back, we'll open up IntelliJ
and get some practice with interfaces.
3:44
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