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 set the stage for interfaces and learn about TODOs!
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
Now that we know what interfaces are,
0:00
let's look at an example to
see when we should use them.
0:02
Let's start by creating a new project.
0:05
Then let's click Next.
0:09
Select the template, hit Next again and
for the project name,
0:11
let's just name it interfaces and
click Finish.
0:17
Next, now I'm gonna put this
into full screen mode so
0:25
it's a little easier to see.
0:28
Getting back to the example,
let's pretend we're part of a team
0:32
trying to create a game where the player
can chat with all the people in the game.
0:35
To do this we'll need a person class and
it'll need a chat method.
0:40
In the project pane let's
click into our project.
0:44
Clink into the source folder and
right-click on our package.
0:50
Then let's pick New> Java Class,
and name it person
0:56
Inside the person class,
let's make sure every person
1:03
has a name by adding a string
property called name, string name;.
1:08
And then let's add a constructor
which sets our name property.
1:15
So Person, and we'll need to
take in a string called name and
1:18
inside, we'll set this .name=name.
1:25
Finally at the bottom,
let's declare the chat function.
1:29
Let's make it return a string,
1:33
name it chat And
1:37
inside the function,
let's add a return and
1:42
say, Hi, I'm a person.
1:48
From here we could build
out a whole host of people.
1:53
But let's just focus on one,
the shopkeeper.
1:56
Over in the project pane
let's make another new class.
2:00
And name it shop keeper.
2:06
Then let's make this class extend
from person, so extends person,
2:12
and use Alt+Enter to add the constructor.
2:17
Below the constructor, let's use
Ctrl+O to override the chat method.
2:21
And instead of just calling
the person's chat method,
2:28
let's return Hi, I'm a ShopKeeper.
2:32
In addition to being a person,
2:38
the shopkeeper also has
a shop to take care of.
2:40
But since we're keeping things simple,
let's ignore most of the complexities of
2:43
running a shop and just give the shop
keeper one function for selling goods.
2:47
Let's add some space at the bottom and
2:52
type void sellGoods and
just leave it as an empty function.
2:54
If this were a real project, you might
want to leave yourself a reminder to
3:02
come back and implement this method.
3:06
In IntelliJ, a good way to leave
yourself notes like that is with a todo.
3:08
To use a todo, just add a comment and
type todo as one word.
3:13
It doesn't matter what case you use, so
feel free to type it however you'd like.
3:18
And if you'd like to add a message
to the todo, just add a space or
3:24
a colon, and then add your message.
3:28
For this one,
I'll say Implement the sellGoods method.
3:32
TODOs are managed down
here in the TODO panel.
3:40
Here you can see all of your TODOs and
quickly jump between them.
3:44
If you don't see the TODO panel,
just click down here to hide or
3:48
show the border options.
3:52
Now that we've got our ShopKeeper class,
3:56
it's not hard to imagine how we
would do this with other classes.
3:57
We might have a Doctor class
that says something corny and
4:01
has various methods for
healing the player.
4:04
Or maybe a bartender class that says
something friendly and serves drinks.
4:07
With the person class,
4:12
we've got a lot of flexibility in
creating new types of talking people.
4:13
But one thing we can't do
is create a talking teacup.
4:18
We can't even create a talking dog.
4:22
I know, it's a bit silly but
it's also realistic.
4:25
Requirements change frequently to
include things we never expected and
4:29
it's our responsibility to
handle those changes gracefully.
4:34
To that end why don't try
making a teacup class
4:38
that's capable of
chatting with the player?
4:40
If you need any help getting started,
4:43
start by creating a new interface
to hold the chat method.
4:45
I'll show you how I solve
it in the next video.
4:48
Good luck.
4:51
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