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 Java Data Structures!
You have completed Java Data Structures!
Preview
We'll talk about how to move an object between different classes up and down the inheritance chain.
Copy/Paste
Movie movie = new Movie("Jurassic Park", "Adventure", LocalDate.of(1993, 6, 11));
Definitions
- Type conversion / type casting / type coercion (See 'Casting Objects' section): different ways of, implicitly or explicitly, changing an entity of one data type into another.
- instanceof (See 'The Type Comparison Operator instanceof') - compares an object to a specified type.
- RuntimeException - unchecked exceptions and all exception classes that inherit from here. They do not need to be declared and can happen at any time.
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
Another thing you
should know about inheritance is that it
0:00
allows you to use an instance anywhere
one of its superclasses is required.
0:03
Now for example, if there was a method
that took an object as a parameter,
0:07
we could pass it our movie class,
and because movie is an object,
0:11
everything would work out
fine, implicitly.
0:14
It's possible to also go the other way
and have a parent class
0:17
redefine its type
or explicitly cast itself as a child.
0:20
The switching of a variable
between types is called type conversion,
0:25
or type coercion,
or most commonly known as typecasting.
0:28
Let's pop open JShell in workspaces
so you can get some
0:32
hands-on experience with these concepts.
0:35
Okay,
so let's open up our handy-dandy JShell,
0:38
and I'm going to just open example.java
here, since we've got our class
0:41
and local date imported there already.
0:45
So now we have
0:53
it and let's make a new one. So again,
that's the class type is movie
0:54
and the name of the variable will be movie
we'll make a new movie
0:58
and we'll say the title is jurassic park,
1:02
the genre is adventure
this is that's a hard one
1:08
it definitely spreads
across multiple genres but this works.
1:11
And we'll say local date dot of 1993
1:14
June is 6 and the 11th. Great.
1:19
My favorite movie theater moment as a kid.
1:25
All of this data is in the teacher's
notes below for you to reference. Cool.
1:28
So we have a new one.
1:31
So one thing we can do to show off the
upcasting or casting up the inheritance
1:33
chain is by simply just defining
a new variable of one of its superclasses.
1:37
So in this case, let's do a superclass
or the parent
1:42
class of the movie, as we know is object.
1:45
So if we just make a new object,
let's call it obj,
1:49
short for object,
and we'll just push movie into it.
1:52
There we go.
1:57
So look, we created a new object and it's
storing movie, but it's an object.
1:58
It's not a movie.
2:02
So that just implicitly worked.
2:03
And that's because movie is an object.
2:05
Now, what if we went the other way?
2:08
Let's do a downcast,
that is going down the inheritance chain.
2:10
So let's make a new movie
and let's just try to push in that object
2:14
and see what happens.
2:17
it says error incompatible types
2:21
java.lang.object cannot be converted.
2:25
So what we need to do
is a specific type coercion, or a type cast,
2:28
and you do that,
basically it's the same thing but
2:32
with the name of the class in parentheses
in front of it here.
2:35
So what this is saying
2:44
it's saying cast object as a movie
and store it inside this variable.
2:45
There we go, bam, and there it is.
There's a new one that got created.
2:50
Now in
2:54
commonplace where we might run into
problems is around methods that
2:55
return objects or even arrays of objects,
as anything could be in there.
2:58
Here, let's make an array of objects.
3:03
So we do an object
3:05
make it an array
and let's just call it some stuff.
3:07
And we'll put the original movie
that we created in there, and let's
3:11
just throw a string in there too.
3:14
So we have a two element array.
3:19
One is a movie and one is a string.
3:21
Let's assume that we give someone this
array, and they think it's just movies.
3:24
So they're just going to go ahead and cast
everything that's in there as a movie.
3:27
So they'll say some stuff, zero,
3:34
that first element, and we'll do get title
on that and call that out.
3:36
So that's how they might use it.
3:41
And then they'll say, oh,
just get the second movie's
3:44
title in there too.
3:46
Oh, and I'll get that.
3:51
So it's a java.lang class cast exception.
3:53
Java link stream cannot be cast
to com.teamtreehouse.movie.
3:57
So there's a way to avoid that,
4:03
and the way that you avoid that
exception is to remember to check
4:05
using a keyword called instance
of before performing a downcast. So again,
4:08
so we have some stuff zero
and we can say instance of
4:14
and if it's an instance of a movie
it will return true.
4:18
So let's see if it's the
instance of a string. False.
4:22
Okay cool.
So let's take a look at the second one,
4:28
which would have saved
us some time if we knew that.
4:31
Okay and seeing that
4:38
it was a string
and that it was not a movie, awesome.
4:39
So as you can see,
you can always safely upcast.
4:42
But when you're downcasting,
if you're uncertain of the exact type,
4:45
you should always make sure
4:48
that your cast is correct
by using the instance of keyword.
4:49
So now that we know how to upcast
and downcast and check types
4:53
using the instance of keywords,
let's continue our Java history lesson.
4:57
There was a point in time
where the common practice
5:02
to make your methods more generic
was to define parameters
5:05
to use as an object, as well as return
objects or arrays of objects,
5:08
and leave the responsibility
5:12
on the caller of the method
to figure out the explicit typecasting.
5:14
Now this works, because as we said,
everything is an object,
5:18
So therefore, any instance of literally
anything could go in and out
5:21
of that method, thus making your method
serve any and all objects.
5:25
Well, there's a bit of a problem
that rears its head.
5:30
If that instance of check
that we just learned about
5:33
was forgotten,
a runtime error is encountered.
5:36
Now, runtime
errors are very hard to catch, so
5:39
the community decided to embrace
a great way to handle the needs
5:42
to make more methods more generic
in a more type-safe way.
5:45
And they are called,
as you might imagine, generics.
5:49
We'll cover generics later in the course,
but next I want to show you
5:53
the power of Java interfaces.
5:56
Unlike with classes,
interfaces support multiple inheritance.
5:58
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