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 explore the methods of the Object class.
Hopefully by this point,
0:00
it's starting to click that
everything in Java is an object.
0:01
But what sort of functionality
do we get from the object class?
0:04
Up in the main method,
let's clear out what we've got and
0:08
start fresh with a new dog variable.
0:11
Dog, name it dog = new Dog.
0:14
Then let's add a line and
type dog., to look at our options.
0:18
Here, everything except for
the sound field, and
0:22
the makeSound method comes
from the object class.
0:27
Now, most of these you don't
need to worry about, but
0:32
there's a few you really should know like
equals, hashCode, toString, and getClass.
0:36
Let's start with the getClass method.
0:42
When you call the getClass
method on an object,
0:45
it returns a class object that contains
information about the class itself.
0:48
Like the class name and
what package it's in.
0:54
Moving to the toString method,
when you call toString on an object,
0:57
it returns a string with
information about the object.
1:02
So if we wanted to print
out what was in an object,
1:06
we would use the toString method.
1:09
Hashcode is a little different.
1:11
When you call the hashcode
method on an object,
1:13
you get back an integer
representing that object.
1:16
However, the important thing is
that the hashcode method will
1:19
return a different integer for
every object.
1:23
So one way to check if an object is
equal to another would be to check
1:26
their hashcodes.
1:30
If they've got the same hash code,
they're the same object.
1:32
The other way to check if two objects are
equal would be to use the equals method.
1:35
Which returns a boolean indicating
if the two objects are equal.
1:40
Awesome!
1:44
Back in the code,
lets get some practice with these methods.
1:45
First let's print out the result of
calling toString on our Dog object.
1:49
Let's delete this dog.,
and then type sout,
1:54
and print out dog.toString.
1:59
And then, let's run the app,
and there we go.
2:03
We've got some information
about our object.
2:08
Looks like we've got the class
name followed by an at, and
2:11
then some letters and numbers.
2:16
Let's take a deeper look at the toString
method to figure out what's going on here.
2:19
In IntelliJ, whenever you want to
take a deeper look at something,
2:24
you just put your cursor on it and use
Cmd or Ctrl+B to jump to its declaration.
2:29
Let's click on toString and then hit Cmd
or Ctrl+V, and I'll hide the run pane.
2:35
Here we have the toString
method from the Object class.
2:42
And if we look inside,
we can see that it's returning get class
2:46
dot get name to get the class name,
followed by an at sign.
2:51
And then it looks like that last bit
was just the hash code represented as
2:56
a hex string.
3:01
Also, sometimes when you use Cmd or
Ctrl+B,
3:02
if you're lucky, you'll get some
documentation like we did here.
3:05
We already know most of this
just from reading the code.
3:12
But let's take a look at this bit.
3:16
It is recommended that all
subclasses override this method.
3:18
Override?
3:24
What does that mean?
3:25
We'll find out in the next video.
3:27
You need to sign up for Treehouse in order to download course files.
Sign up