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 what it means for two Objects to be equal
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
When talking about objects,
0:00
there's typically two ways
to think about equality.
0:01
There's strict equality, which makes
sure they're literally the same object.
0:04
And then there's looks like equality,
0:10
where as long as the two things have the
same properties, then they are the same.
0:12
Here's an example.
0:16
Let's say we've got a playing card
class with two fields, one for
0:18
the number on the card and
one for the card suit.
0:22
Then let's create two new cards and
make them both a two of clubs.
0:25
By default, Java uses strict equality,
0:30
meaning these two cards are not equal and
would have different hash codes.
0:33
But in some situations,
you might want these to be equal.
0:38
I mean, after all,
they are exactly the same card.
0:42
In this case, rather than basing equality
on them literally being the same object,
0:46
you'd want to base equality on
the properties of that object instead,
0:51
which can be accomplished by overriding
the equals and hash code methods.
0:56
Let's see how to do this
with our animal class.
1:01
But first,
let's start in the main method and
1:04
see what we get when we compare two
dogs that are exactly the same.
1:06
Let's delete the findFood line,
And then rename this to dog1.
1:11
Then let's add a dog2 on the next line.
1:19
Dog dog2 = new Dog.
1:22
And let's finish up by printing
out whether they equal each other.
1:27
So S out, dog1.equals(dog2).
1:30
Then let's run it, And
it looks like they're not equal.
1:38
Awesome, now we need to change our
animal class so that equality will be
1:44
based only on the properties of the class,
which, for us, is just the sound property.
1:49
To do this,
we'll need to override the equals and
1:55
hash code methods to be
based on the sound property.
1:58
However, this typically
isn't done by hand.
2:03
So let's use the shortcut.
2:06
Add some space at the bottom of the class,
and then hit Command+N for
2:08
Mac, or Alt+Insert for
Windows to bring up the generate dialog.
2:14
Then pick equals and hashCode,
and just keep hitting Enter.
2:18
Nice, let's run the code again.
2:24
And now, since they have the same
sound property, they're the same dog.
2:30
So I guess that means if we
ever want different dogs again,
2:35
they'll need to make different sounds.
2:38
How about instead of all dogs saying bark,
2:40
we update the dog constructor
to take in a sound.
2:43
In the dog class, let's add in
a sound parameter to the constructor.
2:49
String sound, and
let's update the call to super
2:54
to just pass along our sound parameter.
2:59
Then let's fix the errors
in our main method
3:03
by making dog1 say bark and dog2 say woof.
3:08
And if we run it again,
We're back to having different dogs.
3:13
There's a ton you can do with objects and
inheritance.
3:21
In fact, I wouldn't be surprised if you
start thinking of the world as a bunch of
3:25
objects, with all sorts of
different family trees.
3:30
And for that, I apologize, but
that's just the life of a developer.
3:33
Until next time.
3:37
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