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
All objects we create in Java inherit from a base class named Object. That class provides methods you should know about. Let's explore inheritance and overriding.
Resources
Definitions
- A class that is derived from another class is called a subclass (also a derived, extended, or child class).
- The class from which the subclass is derived is called a superclass (also a base or a parent class).
- Polymorphism - The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.
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
So in order to make our new movie
objects have a custom string
0:05
representation, we first need to discuss
an object-oriented principle
0:08
that we haven't touched on.
0:13
And that concept is known as inheritance.
0:14
Now the main idea is
pretty straightforward.
0:17
Any class may define
that it is a subclass,
0:20
also known as a child
class, of another class.
0:23
Now the class that the child class
is inherited from
0:27
is called the superclass,
or the parent class.
0:29
By doing this, the child class inherits
0:33
the public fields
and methods from its parent's class.
0:35
We'll explore inheritance
in more detail here shortly.
0:38
But the main reason I want to talk about
this now is because no matter what,
0:41
all classes in Java
inherit from a special type called object.
0:45
All of the methods available on object
are available to every class we create.
0:50
Let's go explore this new information.
0:54
Okay, so let's
0:59
go and check out the Java object
documentation.
1:00
Let's do a search for Java object.
1:03
Great, there it is, the first hit.
1:08
Let's go take a look.
1:10
So these are the methods that all objects
that we create will inherit.
1:14
Let's take a look at the toString method,
which is right here.
1:18
It says returns a string representation
of the object. In general,
1:22
the toString method returns a string that
textually represents this object.
1:27
This sounds exactly what we want
1:31
The result should be a concise
but informative representation
1:34
that is easy for a person
to read. Totally what we want.
1:38
And it is recommended that all subclasses
override this method.
1:41
That's exactly what we want to do.
So in order to override a method
1:45
you just create a method with
the same signature. So this signature here.
1:49
You'll see that it's a public method
that returns a string
1:54
and that takes no argument.
So let's go make that signature.
1:57
So in our class, let's say public
2:05
string to string.
2:09
All right,
2:12
now comes the fun part, we have to think
about what we want to write in here.
2:12
So let's return
a formatted string with string.format.
2:16
If you're unfamiliar,
we can use this the same exact way
2:21
that we use printf with system.out.
2:23
So let's say that it's a movie
2:28
and a new line.
2:33
Let's put title here and percent s.
2:34
Then we'll put the genre, percent s.
2:40
And finally
2:45
the release date, %d.
2:46
Okay, let's plug in our member variables.
2:50
And here,
I think just the year is perfect.
2:59
Luckily, there's a method called
getYear available on local date.
3:01
Alright,
so one more thing that we should do.
3:08
Maybe we should surround the title
with quotes, but how would we do that?
3:10
Because there's already
double quotes there
3:14
Obviously, if I put double quotes
3:17
here, that's gonna ruin this
the string layout.
3:19
So what you can do is
you can escape double quotes
3:22
when you want to print them out.
So let's go ahead and do an escape that.
3:24
And then I'm going to do one on this side
escape the quote.
3:29
So basically that's saying
this is not ending the string.
3:33
This is actually really writing the quote
out. All right, so let's save that.
3:36
Let's go back to example and remove
that first part of the string now.
3:41
There we go. Let's go ahead and
let's compile that and run it.
3:46
Oh there it is!
3:57
So this is a new movie
and there our fancy formatted string.
3:58
You can even see the quotes that we added.
4:02
That is awesome. It's working!
4:04
I want to show you another best practice
and that is using the override annotation.
4:07
An annotation is used to help
the compiler, at compile time,
4:11
understand that you're intending
to override the method.
4:15
It doesn't actually change the code.
4:18
It just provides hints to the compiler.
4:20
So let's add a hint
that this is an overrode method.
4:22
So we're going to say, at override.
4:25
Now what happens is
if the parent class doesn't have that
4:28
method, you'll get a compile time error,
which is great.
4:31
So watch this. Let's misspell toString.
4:34
Let's make this toString cheese.
4:37
And I'm going
to go ahead and compile this.
4:40
We'll get an
4:46
error and it says
method does not override or implement
4:47
a method from a supertype and that's
because the string is spelled wrong.
4:50
So let's go ahead and fix that back.
4:54
Try to run that one more time.
5:00
And there we go.
5:07
We have it back.
5:08
Awesome, we've overrode an inherited
method that all instances have.
5:09
Awesome job.
5:13
Alright, so let's review this
with a little graphical help.
5:14
So we built our class,
or Blueprint, for Movie,
5:19
and it exposes some methods getTitle,
getGenre, etc.
5:22
Now, like all classes in Java,
they automatically inherit from the base
5:26
object class.
5:31
Well that object Blueprint also exposes
5:33
some methods like toString and equals.
And like we saw, those
5:35
those
5:39
methods are part of the movie blueprint
without us having to do anything at all.
5:40
We do have the option to override them,
as we saw, and perform
5:44
our own version of that method.
5:48
When we do, we should always
5:51
mark it as such, just in case
the parent changes its method,
5:52
then we'll be notified through warnings
and compilation errors.
5:56
We can also define specific
6:01
inheritance ourselves,
and it follows the same logic.
6:03
We'll do some of that
in an upcoming course.
6:06
But first, I'd to explore,
just a tad, what this inheritance
6:08
relationship allows us to do
and some of the hidden pitfalls.
6:12
So why would we ever want to do this?
6:16
Well, as it turns out,
there are many times in reality
6:18
where things
this inheritance actually happen.
6:21
And modeling it this way, of course,
helps us to not duplicate efforts
6:24
as well as creating the understanding
of how the types interoperate.
6:28
An example that I find super useful
is the one in the Java SE tutorial.
6:33
In the example, they defined the class
bicycle, which defines its own methods.
6:37
But as we all know,
there's different types of bicycles.
6:42
There's mountain bikes, there's road
bikes, low rider bikes, and many more.
6:45
And each is a little different
in its own way.
6:49
So what they do in
the example is extend the bicycle class
6:52
and create child classes for mountain
bike and road bike, etc.
6:56
That way, they have all the functionality
of the superclass, Bicycle,
7:00
but are able to define
what makes each class different.
7:04
This process is called polymorphism,
which is Greek for many forms.
7:07
If you want to, you can read more about it
in the teacher's notes.
7:12
Now, I wanted to give you a little mental
trick, too.
7:15
You can spot a possible inheritance
situation every time you hear the phrase,
7:17
"is a".
7:21
For instance, movie is a, or in
this case, is an, object.
7:22
Now, with that information,
your next exercise is a code challenge.
7:28
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