This course will be retired on July 14, 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
Well done!
You have completed Inheritance in Java!
You have completed Inheritance in Java!
Preview
In Java every class either directly or indirectly inherits from Object. We'll explore more about what this means in this video.
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
We just saw how we can use
the extends keyword to use
0:00
one class as a foundation for another.
0:03
But whenever you see the extends keyword,
I want you to also think of the word is.
0:06
So from our first example, a B is an A.
0:12
Or more recently, a dog is an animal.
0:16
Or for a bigger example, a car, a bus,
and a motorcycle are all ground vehicles.
0:20
While planes and
helicopters are air vehicles.
0:26
And they all have a common
base class with vehicle.
0:30
So a car is a ground vehicle and
is a vehicle.
0:33
Okay, it's time to let you
in on a little secret.
0:39
Everything in Java is an object.
0:42
When you create a new class, by default
it extends from the object class.
0:45
So technically, our family trees
should've looked more like this.
0:50
And technically everything
in Java is an object.
0:54
Back in the code, let's take a deeper
look at what all this really means.
0:57
First off, let's look at our animal class.
1:02
Even though it doesn't explicitly extend
the object class, it still extends object.
1:06
It just happens by default.
1:12
So, if we try to make
it extend from object,
1:15
we'll get a warning that
that's not necessary.
1:19
And using Alt+Enter to fix it just
takes us back to where we were, okay?
1:24
So if animal extends object,
1:30
that means our dog is an animal,
is an object.
1:33
So up in the main method,
1:38
instead of declaring out
dog variable as a dog,
1:41
we could declare it as an animal or
even an object.
1:47
Though if we told Java that our dog
is an object, we'll lose the ability
1:52
to call makeSound, since the object
class doesn't have that method.
1:58
Luckily, if we're ever in this situation,
2:03
we can get our dog back by
using something called a cast.
2:06
But before we talk about casts,
2:10
let's see how we might end up with
a dog that identifies as an object.
2:13
Let's say we're going out of town.
2:18
And we're planning to leave
our dog with a friend.
2:20
Naturally, we'd need to
make the list of things
2:23
to give to our friend when we leave.
2:26
Now, just to keep things simple,
2:29
let's say the only things on this
list are the dog and some dog food.
2:31
Back in the code,
let's try to make this list.
2:37
We already have a dog class.
2:40
So, at the bottom, let's add a DogFood
class and just leave it empty.
2:42
So class DogFood {}.
2:47
Then, up in the main method,
instead of creating a new Dog object,
2:52
let's delete this line, and instead,
create an array of objects.
2:58
Let's type object, and
then make it an array and name it list.
3:04
Then let's set it equal to an empty array
by adding brackets and a semi colon.
3:10
Finally, back inside the brackets,
3:18
let's populate it with a new Dog
object and a new DogFood object.
3:22
Remember, since they both
share Object as a base class,
3:29
we can put them in the same array
by making it an array of objects.
3:34
Awesome, now let's take a quick break,
and when we get back,
3:40
we'll see how we can get back to
calling makeSound on our dog object.
3:43
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