A class can override methods that it inherits.
You saw that the object class is the common ancestor of every class.
So every class inherits methods from the object class.
Now, a class can customize, it can override methods that it inherits.
So in this lesson, you're going to learn to override the equals method.
When the double equal operator is used for objects, it compares the references, it returns true if
the references are equal.
So inside main job, a person to equal to person.
And then check if person to double eagles person.
Run the debugger.
Person to any person share the same reference, so the double equals sign returns true.
Now what if I said person two equal to a copy of person?
We run the debugger.
Person to store is a reference that points to a new object, and because they don't share the same reference,
then the operator is going to determine that they're not equal.
That being said, never used a double equals sign to compare objects.
Remember, that object is the ancestor of every class.
So by default, our person class is going to inherit this equals method.
Let's use it and see what happens.
We'll check if person two equals person one.
Run the debugger.
They don't share the same references, they do share the same fields.
But it returns false.
If you have another look at the equals method that we're inheriting, it returns true only if the references
are equal.
So hear us saying this is no different than saying this person two equals person one.
You can even confirm this, relaunch the debugger.
And it steps into the equals method inside the object class.
And you can see that a default equals method that we're inheriting compares the references of two objects.
That's why we need to override the equals method.
Override means to change the logic of an inherited method, you're going to customize, you're going
to override the equals method for the person class.
You already know how to override an equals method.
You've done it a million times before.
All we have to do is replicate the signature of the equals method we're inheriting.
And here we can write to whatever we want, we could print high and return false.
But the point is, we overrode the equals method and JAV is now going to use this one instead, try
it out, run the debugger and it calls the equals method we overrode.
Now, there's a much easier way to override equals, just the right equals, and let Intellisense autocomplete
the method for you and beautiful.
Don't worry about what super is for now remove it.
And now we can customize the equals method, just like we always do, if the object being passed in
is No.
Return false.
If it's not an instance of the person class return false.
If it is typecast, the object to person.
And then compare the fields of the current object that's calling this method to the one that's being
passed in return, the Stone Age equals person H.
We're going to have to add some Gedda's first, let's do just that.
And good.
Now, the override annotation means we're overriding a method that we inherited.
You don't have to include it.
Remember, in the past, we never did, but you should leave it.
Because if you misspell the method, it's going to throw an error and say there's no method equal to
override, so that's good to have.
Now, if you rerun the code, the person object calls the overwritten equals method.
Let's recap, you overrode the equals method.
Override it means changing the logic of an inherited method when you override a method, Jarvis starts
using that one instead.