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 what it means to 'override' a method and see how to use the 'super' keyword.
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 finished talking about
a few methods that are available to
0:00
every object, but
0:04
we got a little confused when the toString
method said that it should be overwritten.
0:05
In Java an override is when a child
class has a method with the exact same
0:10
signature as one in the parent class,
but with a different implementation.
0:15
It's like saying yes,
I do have this method,
0:20
but I'd like to handle it
differently than my parent does.
0:23
Think about all the things you do
differently than your parents,
0:27
that's overriding.
0:31
Let's get more familiar with overrides
by overriding the toString method in
0:32
our animal class.
0:37
Let's close object.java, and
back in our animal class, let's make a new
0:38
toString method with the same signature
as the one from the object class.
0:44
So it should be public return a string,
0:51
be named toString, and
not have any parameters.
0:54
Then let's add a method body and inside,
0:59
let's return what type of animal
this is and what sound it makes.
1:02
Let's type return and
then to get the name of the animal,
1:07
let's use the getClass method,
and then call .getSimpleName
1:12
which returns the class
name without the package.
1:17
Then let's add a + and in quotes,
let's add a ": sound =
1:21
"and then we'll just add our
sound variable, and a semi-colon.
1:27
Then let's run the app, And
now, instead of getting
1:34
the nonsense we got before,
we get something useful, awesome.
1:39
However, we're not done yet,
there's still one thing missing, and
1:44
that's the @Override annotation.
1:48
Let's add a line above our toString
method, and type @Override.
1:51
Annotations like these aren't strictly
required, but you should always do it.
1:58
It lets Java know that we're attempting
to override an already existing method.
2:03
So if we change the name of the method to,
getString,
2:08
it'll let us know that the method does
not override one from its superclass.
2:16
We'll learn more about annotations later.
2:24
But for now,
just know that when you override a method,
2:26
you should add an @Override annotation.
2:30
Okay, let's change that back to toString.
2:32
And then, I want to show you another
way we could have done this.
2:36
Let's copy our return line, And
then delete this whole method.
2:40
And I'll hide the run pane as well.
2:46
Then, let's use Ctrl + O
to open the override menu.
2:53
And let's arrow down to
the toString method and accept it.
2:57
Then let's paste in our return line,
and we're done.
3:01
Intellij has tons of little
shortcuts like this, and
3:05
while I'll do my best to show them off,
that doesn't mean you have to use them.
3:08
There's nothing wrong
with just typing it out.
3:14
Though did you see what it
defaulted to for the return?
3:16
Let's use Cmd or Ctrl + Z to undo and
take another look at this.
3:20
It looks like it's
returning super.toString.
3:26
In Java, the super keyword
refers to the parent class,
3:30
meaning this is calling the toString
method of the object class.
3:34
Okay, let's redo our paste, And
3:40
then let's see why we
need the super keyword.
3:44
Let's say that every time a dog makes
sound, it should also wag its tail.
3:49
To do this, we'll need to override
the makeSound method and our Dog class.
3:55
Let's add a line at the bottom
of the Dog class, and
4:00
use Ctrl + O to override
the makeSound method.
4:04
Then let's a line after the call to super,
and
4:08
type sout, And in quotes,
let say *wags tail*.
4:13
And now, when we call the makeSound
method on a Dog object,
4:20
it'll first call animals make sound
method and then print out wags tail.
4:23
Let's try it up in the main method.
4:29
Let's delete line 7 again, And
4:33
then call dog.makeSound.
4:37
And if we run it, Perfect,
that's just what we were expecting.
4:41
Another place you might see the super
key word is in a constructor.
4:47
Let's see how that works by making
sure that every animal has a sound,
4:50
by requiring it and the constructor.
4:55
Let's start by adding a constructor
to the animal class and
4:57
making sure it takes
on a string perimeter.
5:01
So Animal, and in parenthesis, it'll
take in a string variable called sound.
5:06
Then let's add the brackets,
and we've got our constructor.
5:13
Inside the constructor,
5:16
let's update our sound field to
equal the new sound parameter.
5:18
this.sound = sound, nice, but
5:22
now we're getting an error
in our Dog class.
5:27
Since an animal now requires
a sound in its constructor, we
5:32
need to call through to that constructor
in order to have a valid animal.
5:37
To do that, you just use the super key
word followed directly by parentheses.
5:41
So instead of setting
the sound equal to bark,
5:47
we want to call the constructor
in the animal class by using
5:51
the super key word and
passing in bark for the sound.
5:55
Let's run it one more time and
make sure it still works.
6:00
Awesome, being able to use overrides and
6:08
the super keyword can really help
turbocharge your programming, and
6:11
give you a lot more options for
how to build an app.
6:16
Though giving developers more
options isn't always a good thing.
6:19
More on that in the next video.
6:23
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