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 learn how to use abstract classes.
This video doesn't have any notes.
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
Inheritance is a powerful concept and
0:00
it gives us a huge amount of
freedom with how we develop apps.
0:02
However, sometimes, it makes sense
to limit some of that freedom.
0:06
One place we might want to add
a restriction is with our animal class.
0:11
Right now, if we wanted to,
we could create a new animal object.
0:16
This might not seem like an issue,
but think about it.
0:21
What does a generic animal look like?
0:24
If I asked you to draw an animal, you
wouldn't draw some amorphous generic blob.
0:27
You'd draw a specific animal,
like a goat or a tiger.
0:32
You can't just create an animal.
0:36
It has to be something more specific.
0:38
In Java, this sort of class
is called an abstract class.
0:41
And to make an abstract class,
0:45
you just write the word
abstract before the word class.
0:47
But before we make the magic happen,
0:51
let's look at the two things you
need to know about abstract classes.
0:53
1, abstract classes
cannot be instantiated.
0:57
You can't use the new keyword
with an abstract class.
1:02
And 2, abstract classes
can have abstract methods,
1:05
which are methods without a method body.
1:09
You can think of an abstract method as
pretty much just a forced override.
1:12
It's like saying, if you inherit from me,
you have to override this method.
1:18
Looking back at our vehicle example, the
vehicle class would be an abstract class,
1:23
and inside the vehicle class we might
have an abstract method called move.
1:29
This way,
all vehicles will have a move method, but
1:34
we'll leave it up to the specific
vehicles to tell us how they move.
1:37
All right, let's get back to the code and
get some practice with abstract classes.
1:42
Let's start by making our animal
class abstract by adding the word
1:47
abstract before the word class.
1:52
Then, up in the main method,
1:55
let's see what happens when we
try to create a new animal.
1:57
Let's add a line at the bottom and
2:01
then type Animal animal = new Animal();.
2:04
Which gives us an error that animal is
abstract and cannot be instantiated.
2:09
Nice, let's delete that line, and
2:15
then let's try creating an abstract
method called findFood.
2:18
Since all animals need to find food,
but they all do it differently,
2:23
this is a good place for
an abstract method.
2:27
Let's add some space after the
constructor, and then declare our method.
2:30
Start with the abstract keyword,
then add the method signature,
2:34
so void, findFood, with no parameters, and
2:40
instead of adding a method body,
just add a semicolon.
2:43
Now that the animal class
has an abstract method,
2:48
we'll need to override that
method in our dog class.
2:51
Luckily, Intellij already
knows how to do this.
2:55
So we can just use Alt+Enter
to implement the method.
2:58
Finally, to finish the method, let's print
out that the dog looks at its human and
3:04
then call the makeSound method.
3:09
So sout, and we'll say, looks at human.
3:11
And then call makeSound.
3:20
Perfect!
3:23
Now to test it, back in the main method,
3:25
let's call dog.findFood
instead of dog.makeSound.
3:28
Looks like I need to go feed the dog.
3:37
But before I go,
3:39
there is one more gotcha about objects
that we should really talk about.
3:40
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