This course will be retired on June 1, 2025.
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 create and implement the 'Chattable' interface!
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
All right,
were you able to create a talking teacup?
0:00
Either way, here's how I did it.
0:03
First, we need to make an interface
to hold the chat method.
0:05
To do this,
I'll right click on the package, pick New,
0:09
Java class, then I'll name it Chattable.
0:14
Change the kind from class to interface,
and hit Okay.
0:20
Now that I've got my interface, inside
I just need to declare the chat method.
0:25
So String chat, and there we go.
0:31
But before we move on,
just a few quick notes.
0:37
First, notice this green I icon, which
tells us that the file is an interface.
0:41
Second, even though
IntelliJ makes this look so
0:48
official with the green I icon,
remember these are all just .java files.
0:52
So if we wanted to change
this from an interface and
0:57
to a class, there's nothing stopping us.
0:59
Finally, if you chose a different name for
your interface,
1:05
there's a shortcut we can use to
rename it everywhere in the project.
1:07
This way,
you can just change it in one place, and
1:11
IntelliJ will change it in the rest.
1:14
If you'd like to rename your interface,
put your cursor on it, and
1:17
use Shift+F6 to bring
up the rename option.
1:20
You can also right+click,
refactor, and rename.
1:25
Now it's time to use our interface.
1:31
We need to update the Person class,
update the Shopkeeper class, and
1:33
create our new Teacup class.
1:37
Let's start with the Person class.
1:40
To implement our interface,
1:42
we just need to add implements
Chattable after the class name.
1:44
Then, since all interface
methods are public,
1:50
we'll need to update our Person's
chat method to be public.
1:53
Which we can do using Alt+Enter.
2:00
Also, even though the override
annotation is optional, and
2:03
we don't have any errors,
we should still add it.
2:07
So let's add a line here and
add the override annotation,
2:11
because this method is overriding
from the chat method in Chattable.
2:14
Moving on to the Shopkeeper class,
2:20
all we would need to do here is
make the chat function public.
2:22
However, since we used Alt+Enter
to fix it in the Person class,
2:26
IntelliJ has already fixed that for us.
2:29
Finally, let's create the Teacup class.
2:33
Let's right click on the package,
and choose New, Java Class.
2:37
Then let's name it Teacup, and hit Enter.
2:42
Now that we've got the class,
2:45
let's make it implement our Chattable
interface with implements and Chattable.
2:47
And let's wrap it up by using Alt+Enter
to implement the chat method.
2:53
And let's make out Teacup say
something like, hi, I'm a Teacup.
3:02
Now I'm sure we can all agree that
this code just feels a bit better.
3:08
But to help drive that point home,
3:13
let's look at a couple
examples in the next video.
3:15
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