When you add in equals method, you must always add the hash code method.
In this lesson, you will add an equals method, any hash code method to the children classes.
We're not going to add an equals method or a hash code method to the parent class, it's impossible
to create an object of the product.
There's nothing that can be equal to Serenity's the Java code generators that an equals and hash code
method to the short class.
Auto import, the objects, utility costs.
And this equals isn't complete, it has to compare every field from both objects, but it's only comparing
the size field and I find this a bit awkward.
I'd rather just say this size equals shirt that size.
OK, how do we compare the inherited fields, remember, that super refers to the super class, which
also means the parent class.
Superdog, a method calls a method from the parent class.
So we'll check if Superdog get price.
Equal share target price.
Superdog, get color.
Equal shirt, get color.
And Superjet brand.
Equal shirt, douget brand.
Before we continue, super refers to the parent class superdog, get price calls, the get price method
from inside the parent class and get price is going to return the price value of the current object
that's calling this method.
OK, now here, make sure this has a hash, not hash code, if the generator only sees one field that
defaults, the calling the objects that hash code method, but we need to call it hash because it accepts
many arguments.
And here we have to add the remaining fields using the getas superdog set price superdog, that color.
And Superdog get brand.
The passcode good method needs to always stay consistent with your equals method.
All right, now I can check if shirt and shirt to our equal.
So run the debugger.
This points to the current object that's calling this method of points to the object that was passed
into the equals method, the two objects don't share the same reference.
So this is suspect.
The object being passed in is an instance of the Sherpas.
So it passes this test and you can typecast it and now we can compare the size.
And here super refers to the parent class superdog, get price calls, the get priced method from inside
the parent class.
And get price is going to return the price value of the current object.
The price of that object is the same as the one we're comparing it to, the color is the same, the
brand is the same.
So ultimately equals returns.
True, these objects are equal.
All right, and now I'm going to print the hash good of both objects.
Endure the same Perfect's passcode method must always assign equal objects, the same hash code, this
rule is so important and if you don't follow it, you're going to get bugs on that very same topic.
I want to remind you once again, because this is so important, when you override an equals method,
you must always, always override the hash code method.
If you don't do that, you're breaking the equality contract and you're going to get so many bugs.
Anyway, so we're good.
I want to talk about one more thing before we wrap up this versus super.
This can call local methods and inherited methods.
Super can only call inherited methods.
So going back to our code, let me ask you this, should we use this or super.
Super, we don't want the added flexibility because it's dangerous.
This is perfectly capable of calling an inherited method.
In fact, rerun the debugger.
This points to the current object that's calling the equals method.
The current object inherits a get price method, so it's capable of calling it price get price returns,
the price of the current object.
OK.
But now what if in the future I end up creating a random method that happens to be called get price
inside the shirtless?
And we'll just return three.
Now restart the runtime.
This is going to prioritize the local method.
Even though we wanted to call the inherited method, and that's why we don't want to use this, it's
more flexible, which also makes it more dangerous.
And in my opinion, a good code should protect you from yourself.
So if you put super, the current object is going to call the inherited method no matter what.
So restart the runtime.
Super refers to the super class, the parent class, so super doget price is going to call the get price
method inside the parent class no matter what.
Now, that being said, I always use super to call an inherited method.
But this year can still cause problems, so just make sure to remove it.
All right, now we'll do the same thing inside the pants class, use the Java code generator stat equals
method, any hash code method.
We need to compared the other three fields, so we'll check if superguy price.
Equals penstock get price.
If superdog that color.
Equals pants that get color.
And if Superjet brand.
Equals Penstock get brand.
OK, now here, make sure this has hash, not hash code, because hash can accept many arguments and
we need to pass in three of them.
Superdog get price, superdog at color.
And supermarket brand.
I'm not going to test this code, you can feel free to do so on your own time, it should behave the
same as what we did for the short class.
Let's recap, you created the equals and hash code method inside the children classes.
And Superdog Method calls a method inside the parent class.