- Podcasts
- The Java Developers Hub
- Java Object Class Explained | Master equals(), hashCode() & toString() Description
Java Object Class Explained | Master equals(), hashCode() & toString() Description
The Java Developers Hub
- December 3, 2026
- 1 h 8 min
- Episode 35
About this episodeThe Object class is the foundation of Java's class hierarchy. Every Java class directly or indirectly inherits from it, making it one of…
- Understanding the Java Object class.
- Why every Java class inherits from Object.
- Exploring important methods provided by the Object class.
- Understanding the equals() method.
- Differences between == and equals() in Java.
- How to override equals() correctly.
- Understanding the hashCode() method.
- The relationship between equals() and hashCode().
- Why equal objects must have equal hash codes.
- How HashMap and HashSet use equals() and hashCode().
- Understanding the toString() method.
- Overriding toString() to create meaningful object representations.
- Practical examples of custom Java classes.
- Best practices for implementing Object class methods.
- Beginners learning Java object-oriented programming.
- Computer science and software engineering students.
- Backend developers working with Java collections.
- Java programmers preparing for technical interviews.
- Developers interested in object comparison and hashing.
- Anyone looking to understand Java's internal object model.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Java Object Class Explained | Master equals(), hashCode() & toString() Description
Welcome back Coder Army to our Java series. And today we are going to see a very interesting and very important class. Which we call Object Class. Yes, in our Java there is a class named Object. Now this is a very important class. When we will understand about it, you will know why. But this is also very interesting. So let's go to the screen and explore this class. Yes, so look, we are going to see our Object Class. Okay.
We had read one more thing in java.language package. Do you remember which class was that? That was our system class. System class. Because java.languagePackage is always imported by itself. You don't need to import it by yourself. So we get access to system class. In the same way, we get access to object class too. Because it is imported from the beginning. Okay, we understood that we get object class from the beginning.
All the classes of Java that we make by ourselves or that Java has made by itself, the parent of all of them is our object class. I can write it like this that every class in Java inherits from object class. Okay? What does it mean? It means that let's say if we had made a class of our own. Let's say student. So we used to write such a code class student and whatever we had to do in it. But whenever Java compiles it from inside, then it would write extents.
And this extends our. Let's say a human class. Because a student is a human. So it extends a human. And in Java, we can extend only one class. Multiple inheritance is not possible. Then how will we put an object class here? Or how will Java put it? No problem. Student extends a human. So we will have a human class. Class human. Now if a human doesn't extend anyone, then it will extend that object. Extends object.
Indirectly, our student class is also extending the object. Now, if the human is also extended by someone else, let's say animal, because human is also an animal, then our class will extend the animal object. Animal extend object. This means that there will be a parent class in the hierarchy which will extend this object class. So, directly or indirectly, your student class will inherit the object class.
Now the question arises, why does Java need such a parent class, a root level class? There is a very good reason for this. Look, you think that inheritance and polymorphism can work very well in Java. We know that there will be some functionalities that will be required in every class. We can put those functionalities directly in the object class.
So if we have a class object, class object in this way whatever methods are written in it and we know if we make our own class student then it directly or indirectly inherits this object class so our code will look like this from inside extends student ok so the functionalities in this will come in this too means if I make object of student let's say s1.
I can access the functionalities of this object class by putting a dot on this S1. Because I am inheriting it. So one reason is that by bringing common behavior, I can keep some functionalities in the object class. So that all the classes of Java can have that common behavior. Now what can be the second most important reason for this? The second most important reason for this is that Because our object class is a root level class, our parent class. So the reference of our object class can point to any class. We have studied in inheritance that the reference variable of parent class can refer to the object of child class. So we can write like this, let's say object obj, let's say the reference variable is obj, equal to new student. We can do this. And student is our own class. Let's say we have made a class of our own. We can make it point from OBJ.
Okay. What are the benefits of this? We will see that in some time. But I hope you have understood. Because of polymorphism, Polymorphism. The reference variable of our object class can point to any class. Now we can call a method on that obj. Which method can we call? Obviously we can't call it on the student because obj reference pointer.
So, first of all, we will list down all the behaviors, then we will study them one by one. First of all, we will come to the core methods of the object. Okay. We have mainly four core methods in the object class. One method is called, two string. One method is called, equals. One method is called, hashCode and the name of one method is getClass these are our four core methods which are present in our object class apart from this we have methods for cloning so I will write here cloning for that we have one method whose name is clone apart from that I will make it a little smaller apart from that we have methods for garbage collection for garbage collection Here we have a method called finalize. And we have some methods for threads as well. But here we have some methods like wait, notify and notify all. So these are all the methods that are in our object class. Which we have to use mostly. This time in this lecture, we will focus on its core methods and clone. Finalize has been deprecated a lot. Now we don't use it much. I will tell you why we don't use it. And all the methods of threads, we will study when we will study threading. When we will study multi-threading, then we will understand more. But first we will focus on these core methods. What do these core methods say? So first comes our two string method.
Now you must be able to understand the main motive of this method. To convert anything into a string. How will the signature of this method look in our object class? It will be something like this. Public string because it will convert into a string. And the name of the method is toString. And it will have a default execution inside it. Which the object class has written by itself. Okay. Now what did I say? This two string method. Which the object class has written by itself. What will it give us? It will give us a string representation. String representation.
But first let's understand what the two string method is doing. So, as I said here, it will be that the object class we have, class object, inside that, what did I say? A public string method will be toString which gives us a string representation of a class. So this will be its default representation. Now I said that every class of Java inherits this object class. So if I make a student class of my own, then if I write here, let's say class student and I extend it to the object class. I don't need to write it, because Java will do it internally.
But first let's see what implementation Java has given. That is, if in any class, like in my student class, if I make an object of it, if I make it a little smaller, then you can imagine that here I have made an object of student. Let's say student s1 equal to new student. I make an object like this. So if I do s1. 2string then what will happen? So obviously there must have been some default implementation if I do s1. 2string and I didn't overwrite it myself. Even then I can do 2string because it is in my object class. So its default implementation must be running. So what does its default implementation do? Let's see that. So its default implementation does nothing. It will print the class's name at the rate some hexadecimal code, hexcode. Now, what is hexadecimal code? See, we have read that hexadecimal is the base of 16. So, any random number which is unique for this class, we will see that in a while. So, this will be its default implementation. But if I want, I can give a default implementation of my own. Now, when will I override the method named 2string? I will do it when I want that if my class, if any object, let's say, if I write s1. 2string, So what should be printed on the screen, I want to control it by myself. So I will overwrite it. Let's go to the code first and see how it is used. And then we move on. So again I have made my project empty with the name demo.java. So let's say first of all I make my class student. So I make a student class. And I keep two things in it. Let's say I keep.
So, I have set these two variables. And I can print it if I want. If I write system.out.println and I write s1. name, then my Aditya will be printed. And if I write s1. age, then my 28 will be printed. But if I just do s1. toString, see this is the method, there is no error. Means I have access to 2 strings. How will it happen? This student class internally will be extending the object class. Means it will write some code from inside. Extends object class. Something like this. She must be writing code. So even if I don't write it, it's fine. So let's remove it.
2 string. Let's see what's printed on the screen. student at the rate and a hexadecimal value. It is not necessary that this number should be printed every time. It can be any random number for you. But it will be the same every time. The number which is printed once for this student class will be the same because it has an identity. Because I did not overwrite it. Now I will try to overwrite this two string method. So I will write at the rate overwrite and public string two string. So I am overwriting two string.
toString. Now which method will be called? Now this method will be called. Because I have overrided the method called toString. So let's see what will come on my screen. Now, my aditya comma 28 is here, which I wanted. Now, I can override the two-string method according to myself. Very good. Now, one more interesting thing. When you use println, you don't even need to write two-string. Look, you just write s1 and run it. Then it will also get the same output.
Because println function implements toString from inside. So if you see the functionality of println, then whatever you give it to print on the screen, it prints it in the string format. So by default, it is calling toString from inside. So you don't need to write this. If it is calling toString from inside, then this toString will be called. And whatever I wanted, it will be printed on the screen.
True or false, whether they are equal or not. A boolean variable returns. How is its signature? Signature means how it looks in the object class. So it looks like we have a class which is an object. We have seen two strings. Now let's talk about equals. So I said it will be public. Obviously boolean is returning. So its return type will be boolean. The name of the function will be equals. And it will take some parameter. And here it will have a body. Now what will it take on the parameter? This is very interesting. Look, it will take an object class.
Why are we taking the object of the object class? Because look, where is equals? Equals is in our object class. Look, we have made equals in our object class. Now, again, whose equals will it compare? It will compare between two objects. So, basically, if it comes in the object class, then it doesn't know in hierarchy who is going to extend it. Obviously, every class of Java will extend it. But it doesn't know But it doesn't know by itself who all are going to extend it. Now let's see what is its default implementation. So see, its default implementation compares.
What does it mean? See, it will do something like this. Let's say we made an object named student. So we have a class named student. And we will put the same two fields name and age. Now let's assume I made two objects. One is student s1 equal to new student. Now let's assume it has a constructor. So I have given two values here. One is aditya and let's say 28. And the other.
And assume I have given the same value Aditya 28 Okay, what will happen when I have written these two lines? We know how our heap memory and stack memory work So something like this will happen Let's say this is our heap memory and here is our stack memory So first of all, as I wrote new student an object named student will be created in our heap memory and the reference variable named s1 will point here and it will have two values one will be Aditya There will be two values in it, one for name Aditya and one for age 28 Now when I write new student in the next line, whenever I write new, an object is created again Its reference variable s2 will be here, it will point to it And there will be two values in it, one will be Aditya and one will be 28 Now if I write this in my code, s1. equals Now, obviously, I can call equals on S1 because equals is in object class. And S1 inherits the object, so equals will work. Now, again, what do I have to pass in the parameter? I have to pass one more object. So, I can give S2 in the argument. Now, what is S2? It is a reference of the student. And object class... It comes above the student class. It is a parent class. Now, the signature of equals was expecting the object.
OBJ can point this student. So, I can write this. Now, what will S1. equals S2 do? It will compare their references. It will see whether S1 and S2 are pointing the same object. The answer is no. S1 is pointing this object. S2 is pointing this object. Even if the value inside them is the same But still we will always get the answer as false Because their references are different So by default, what does equals method do? It compares references from inside So here the default implementation will be Compare references So obviously I can use this in my student class But I have to accept that If I have made both the students the same The values of both are the same But if both are different objects Then the answer will be false Obviously, I can override this too for my behavior. If I want this equals method to compare exactly the values, if all the values of two objects are the same, if I call it true, then I can override it according to myself. Currently, its behavior is exactly the same of our equal operator. If I write s1 equal equal s2, we have already talked about this. If I write s1 equal equal s2, then also what would it do?
Here, the equals method is also doing the same. In fact, if you look at the code of this equals method, which is defined in the object class, here, You will see its code. So, it uses this equal equal operator from inside. So, it does something like this. This current reference. Is it equal equal to this obj? If it is, then return true. If not, then return false. So, it is using equal equal from inside. So, the meaning of this equals and this equal equal.
Because we have not overrided it yet. So, let's see this in code. So, again I am in my demo.java class. I had extended student from object. And here I had overrided two string. But let's say, I don't override equals right now. I take a new object, student s2. s2 equal to new student. And because I don't have constructor, I do one thing. I put the same thing in s2' s name as well.
See I can do it like this no problem now what will be printed on the screen I told you it will be a false print because although both in s1 and s2 the value is same now I will close this print ok the value is same but it will compare the references so if we run this code then the answer is false no problem because our operator works like this in fact if I write it like this is s1 equal to s2k If I had written like this, then also my false would have come. Okay? Because my equal equal also works the same way that dot equals does. Now to show this, if we go inside this equals method. This is object.java class and this is its equals method. It is taking object obj and comparing return this equal equal obj. This is also using equal equal operator from inside. Now you have to override this. So you will write at the rate override. I will not go, let the default behavior come. So I will do one thing, public boolean I will overwrite the equals method.
Again it will take an object named obj Object obj, now see here I have taken a reference of object name, for now he must be expecting that I have taken boolean so return something. So for now let's say I have written simply return false. Now to explain to you, now it will work. But assume, can I take a reference of student name here? I can't take it at all, I will get an error.
The method equals of type student must override and implement a supertype method. Because he feels that I am not overriding it now. Because here this reference should be of object and not of student. I had told you this. Why? Because our real equals class, if I go inside this equals, sorry, if I remove this for now, And if I go inside the object equals, it expects an object reference pointer. So I have to give it an object reference pointer. I can typecast it from inside, but I have to pass an object here. Okay, so let's try to override this. So what I have to do is, if the name and age of a student are equal, I can tell it that two objects are equal. So here I am comparing this current object, which I can refer to as this. And I have this obj. So I can do something like this. So first of all, I typecast a student to s equal to this obj. I typecast this to student. Do you know how I typecast? I will write student here. Now it is typecast in s. Now I can compare it like this. I can write return this.name equal equal s.name Means the name of the current object should be equal to the name of this s. And this s is the object of the name obj. I have typed this. s.name and this.age should be equal to s.age. If this is there, then I will return true. If not, then I will return false. So I can do something like this. Now let's see if its value will come true. Okay, now let's run this and see.
I just passed an object named obj, typed it in the student and compared it. Now the mistake is that in this println function I passed s2 which was an object named student so there was no problem But suppose if I passed a student s3 here and I didn't declare it as equal to new student And I passed s1. s3 here So s3 will be null by default So there is an error here that the local variable s3 may not have been initialized Let's do one thing, let's make s3 equal to null Now it is null but I didn't get any error, my code should work But as soon as my code runs, I will get an exception which is my null pointer exception.
Because here my code will come, it will try to typecast this obj in stdent and it will try to remove stdent's s.name and s.age. But you can't remove name and age in null, right? So my code is a bit wrong right now. I will make it std and remove it for now. So the wrong thing is that I haven't checked anything. First of all, I should check that if obj is equal to or equal to null, then I return it as false.
Okay, one more problem is that I am taking the reference of object here and object can be anything. Basically, object is a parent class. So, it is not necessary that I pass an object named student. Let's say, I am passing an object of integer class i equal to 28. Something like this. And I passed i here. This is a wrapper class, my code is still running but if I run it, I will get an exception which is called class cast exception Because when I take this particular I class in this obj and try to type cast it in the student, it will not be able to type cast because the obj is an integer, I have passed it here Here I have passed integer and I am collecting in obj. I cannot collect because it is a parent class. But when I type it in student, it will be issued. So I have to check one more thing that what is coming in obj, is it an object of student type or not?
For this, I have one more method in my object class which we will read further. Its name is getClass. So I will check if obj.getClass is equal to this.getClass. If this is not the case, then return false. Now it checks if both classes are of type student. If we don't type cast, then we will get class classCastException. This is also an exception type. Again, we haven't read the exceptions yet. But when we read it, you will understand what are the exceptions. But now you can understand that classCastException will come if I don't check this. Now see, my code should work. I am passing an integer and If I try to cast it, then my answer will be false. So I can't do this either. So now I have optimized it a little bit. First I checked that the object should not be null. I checked that the class of those two should be the same. So dot get class. Now we will see about get class later. Apart from this, I can do a little more optimization. I can do it like this. If this is equal equal obj return true. Now, what does this line mean? This line means that this obj, if it is exactly equal to this, means both the references are the same. Suppose, if I pass s1. equals s1, right? So, if I pass s1. equals s1, what does this mean? Both s1 will point to the same object, right? Their references are the same. If the reference is the same, then the object will be the same. So, why should I work so hard that I check its get class, type cache it, Then I will try to match their name and age Because they will all be the same So I will directly return true So by doing this I have overrided all the equals With all the optimization Now my record will work Now I can remove this line And this student s3 too And I can leave it like this Okay I hope you understood well How we overrided the equals class Now see Let's go to our next method Which is our very important method That is hashCode. Now again this also comes in our object class. Now what does it do? First of all it returns an integer of an object. It returns an integer value of any object. Now the integer return in hexadecimal, in hexadecimal format. Now, what is a hash code? How do we return an integer? Now, imagine that you have a string Aditya. I am telling you to write a code like this, Aditya gives me a number representation like 234. But it should always be like this that whenever I pass Aditya, I always get 234. But let's say I pass some other string, Rohit.
Number, let's say 345, any random number. But then again, whenever I pass Rohit, I always get 345. These types of functions are called hash functions. Hash functions. In fact, they are used a lot in Java. We will read collection frameworks ahead of us. So, in that, hash map, hash set, They are all using hash functions from inside. So basically, it means to convert any type of object into a number, into a unique number.
Now, obviously, I am writing these numbers in normal decimals, but they are in hexadecimal format. They are not on 10 base, they are on 16 base. But again, you understood the concept of hexadecimal. I need an integer reference of any object. Right? So that I can uniquely identify it. And I can compare it further. Okay. Now let's see how its signature will look. Again, our class is object.
In that, there will be a function named hashCode. So this will be our public function. And what is it returning? Integer value. And its name is hashCode. Public int hashCode. And here it will have some implementation. Default implementation. As I told you, this is used in collection frameworks like HashMap, HashSet, etc. Now, if we don't override it, then our default implementation goes away. And what does the default implementation of hashcode do? It tries to remove the hashcode of our object, let's say student S1. So it will give us its hash value. But here we have a very important rule in java. We have a very important rule in java. Let's understand this rule because it will make a lot of things clear. It says if two objects are equal then their hashes must be equal. So basically their What does it mean? Let's say I have a student named S1 and a student named S2.
What happened in reverse? If their hashes are equal, then it's not necessary that the object is also equal. Now, this is a very important rule of Java. Let's understand this object. Why is it saying this? And what is it saying? I say, let's say, I have a heap memory. And this is a stack memory. I have two objects in this. One is a student object.
Let's assume that both are different objects So my rule is that if s1 is equal to s2 If we compare s1. equals with s2. equals and if s1. equals is true And how did we do true? We overrided equal So what did I do? I added the same name here as well and age is 28 and here also name is added as Aditya and age is 28 and what I did to compare? I compared these two with these two so this rule says that if S1 and S2 are equal then their hash code should also be equal so if I take out the hash code of this object and its hash code is 123 So it should also have 1, 2, 3. It won't have 3, 4, 5 or some other number. But reverse is not true. What does reverse mean? It may happen that in your heap memory, let's say I make another object. In your heap memory, let's say another object is S3. So here I call it S3. And its name is something different. Let's say Rohit. And the age is the same, 28. So, it is possible that its hash should be 123. It should be 123 because obviously, it comes in the first rule. What is the name of these two?
So, their hash code should also be the same. But it is possible that its hash code should also be 123. It is possible. Because what do hashes do? I had told you what hashes are. Let's assume there is a function which will give a unique number to S1, S2, and S3. But the number generated by S1 and S3 may be the same for some reason. Their value may not be the same. How will it find the number? Will it make a formula? S1 will be trying to give a random number to S1 and S3 will be trying to give a random number to S3 But it is possible that the random numbers that it has taken here and here are the same So it is also possible that its hashcode is 123 So even though its hashcode and its hashcode are equal But it doesn't mean that the object is also equal So if we go back to our definition, if two objects are equal, then their hash code must be equal. But the reverse might not be true. It might be true, it might not be true. I can't take a guarantee about this. But we have to take a guarantee about this.
Again, you will get the answer to this when we read our collection frameworks in Java. I will make this smaller so that you can see everything properly. When we read our collection frameworks in Java, then you will understand that the hash sets that we have in Java, hash set or hash map or hash table basically all these data types basically all these corrections that work on hashes if you didn't follow this rule suppose that s1. equals if I write s2 then this is true but s1. hashcode is not true is not equal to s2. hashcode then it will be very difficult. Why? Because in that case, if I am doing this for a student object, and my two objects are S1 and S2, then in that case, my HashMap, HashSet, HashTable will not work properly. Because they will not know if I have to keep S1 and S2 in the same place or in different places. They will not be able to compare them properly. Again, we will do a deep study about this when we will know the internals of HashSet, HashMap, HashTable. But for now, you must follow this rule. That this rule should always be followed. I have already told you how. If S1, S2, S3 are reference pointers. All three have their own objects. And what I have written. If S1 is equal. If S1 is equal to S2. Then their hash codes should also be equal. And reverse might not be true. Okay. Now if we don't override our equals. Then by default it would be true.
If we. This equals method is not overriding So by default the equals method is working And by default we compare references So we always compare references of S1 and S2 Now in this particular case S1 and S2' s references are different Even if their values are same So S1 dot equals S2' s answer is false If this answer is false, then we don't have to talk about its hash code being equal.
But since these two are not equal, then I won't talk about their hash code. But if S1 dot equals S2 true comes, If true would have come in our default behavior, then it could have been the same reason. It could have been the same if s2 would have pointed to this and s1 would have pointed to this. If both would have pointed to the same object, then only our s1. equals s2 would have been true. And in that case, obviously their hashcode would have been true too. Because both the objects are the same. If the object is the same, then if the hashcode is 1, 2, 3, then whether you do s1.
Hashcode, Your answer will be 1, 2, 3 only. Right? So if you didn't overwrite equals, then the condition of this rule would have been true. But because we overwrited equals, so now it's our responsibility to overwrite hash code as well. And if s1 and s2 are equal, then always equalize their hash code as well.
Look. I know that I have to make hash code the same for those two objects whose Equals same, right? Means both the objects are the same. So when will the two objects be the same? If the name of that student is also the same and the age is also the same. Means I have to factor in the name and the age in some way. Now the way to find the hash in the real is to catch the prime numbers. So that we get more unique numbers. Because you know what will be the prime numbers? There will be more chances that they are not the same. So what do we do? We start with an int result. And we keep it non-zero. We try to keep it greater than zero.
We keep increasing the result by multiplying it with the next prime. And once we will factor the name. Means we will add the name. Then again we will multiply the prime in it. Then we will factor the age. See what I am telling you. Then I write int. Again I put it in the result. Whatever is still in the result. which is still in the result, into 31. Again, I took a bigger prime than this. I took the next 31 prime, bigger.
Now I could have taken out the hash code from it But I directly used the name.HashCode, you know why? Because the name is a string and the string itself overrides the hash code So if I go to this hash code, then I will be in the string.JavaHashCode method That means it has overrided the hash code So I will use the hash code of that and finally return it Result. Now I know that unique value will always come out.
So any student. whose name and age are the same, this value will also be the same. And why did I take 17 and 31 here? These are not prime numbers so that I can always try that the hashes of two objects which are not equal are not the same. Now when the prime numbers are multiplied and age and name are added to it, then the chances of both numbers being unique will increase.
Okay. Now, I won't get this exception. Now, my answer will be false. Why false? Now, my s1 is null. Now, if I do s1. equal s2, then equals will handle null. If it handles null, then it will return false. And these hash codes will be unequal. Why will they be unequal? Because one hash code will be on 28 and another on null. And in the case of null, I am taking 0. So, the hash code of both will be different. So, this will be my correct method. So, I will make it null again. Now, this code that I have written from inside, Java does internally by itself.
Now what I did is I took an object class Objects, see this object class is different and objects class is different See where does this object class come from? If I hover it, it comes from java.util package From where our scanner class also comes So here all utility classes are there So objects is also a utility class which has a hash function In that I gave name and age So it will also remove its hash from inside in the same way as I did earlier Ok So see this was my earlier method I am commenting it here And I am saying the simple way to remove is return objects.hash here I will pass name and age now again for this it is important that you import java.util package so import java.util.objects class If you have imported then no problem your code will work and still our same output will come true true. I hope you understood this well. Now let's move on to our next class which comes in our object class which is called getClass. This is also very interesting. Now see what it does. It returns runtime class of an object. What does it mean? Run time class of an object. Look, it means that suppose I have an object of a student. Let's say I wrote student s1 equal to new student. Something like this. If I write s1. getClass So you know what it will bring to me? It will bring me student. S1. getClass means which class I am working on. Basically, which is my runtime class. Now this S1, who is it pointing at? It is pointing at an object of student class.
Actually, S1. getClass will not bring me student. Actually, what will happen from inside? It will bring me a class type object. Yes, in Java, there is also a class named class. Capital class class. There is a class of type in java, like the class named object. Similarly, there is a class named class in java. We will read this when we will read reflections in java because it is a little advanced topic.
Now I will get exactly print here student. Now we will see the class object later when we will read the reflections. But I hope you understood that if I do s1. getClass then it gives me the class object and I name it dot get. Then I get the name of the class which is student. Now let's see its signature. Again I assume I have class object and in that there will be a method called getClass. Now how will it look? The first interesting thing is that this is our final method. I can't override this. Because it is giving us the name of the class and I can't change it. So this is public final. Its return type is class. I told you that class is also a class in Java. And it's return type is not class. I have added a special character to it. It is called wild guards. We will read this in Java later. And then the name of this class which is getClass. And here comes its default implementation. So this is our complete method getClass which gives us an object named class and if we do dot getName on it then we get the name of the class. Now again the interesting thing is that it is final so you can't override it which we just saw. Now let's see it in the program. So again I came back to my demo.java. This was my student class. I overrided two strings in it, I overrided equals and I overrided the hash code as well.
getName Something like this. So what will be printed here? I will comment these prints for now. So let's see what my name is being printed. Very good, my name is student print. And same if I take s2. getClass then also my name will be student print because both are object of student class. Now apart from this I have one more operator in java which I call instance of operator. We haven't read about it yet, but we are reading it here. Instance of operator also does almost the same thing. Let me tell you what it does exactly. It checks if an object is instance of a class or any of its subclass. There is an instance of operator which checks whether any object like S1 is an instance of a particular class or not. So basically what I am saying is, what if I write system.out.println and write S1 instance of student.
Spelling is wrong. So I write S1 instance of student. Now since instance of is an operator. So it will come between S1 and student. It will check that what is this S1 reference variable. This is student's object. So it will return true in this case. So I will comment these two. My answer should be true. Absolutely correct. Now if I check whether S1 is an instance of an object. Now object is a parent class. Now instance of also checks whether S1 is an instance of an object or a subclass of an object. Now object's subclass is student so the answer should be true. Because S1 is not an instance of an object but an instance of a student. But student is an object's subclass so the answer should be true.
Okay? I agree that both are animal references. But which object is being created on the real run time? For A, the object of animal is being created. For D, the object of dog is being created. So when I did a.getClass.getName then animal came. And d.getClass.getName then dog came. But if I ask that system.out.println Is A If the instance of is animal, then also the answer will be true.
Why? Because A is pointing to the animal. And dog is its subclass. It's not a parent class. So A is not a dog instance. It's an animal instance. Okay? I hope you understood the difference between instance of and get class. And where we should use it. Now instance of tells us that an object is an instance of a class or of its subclasses. Okay? And get class tells us which class it is pointing to in runtime.
So the signature of object is Protected object clone throws class not supported exception. And here we have its default behavior. Now this clone method is a bit unique. Java says that you can use this clone method to make a copy of your object. You can either override it or call the default behavior. Let me tell you what the default behavior is. But you can call it. But you can't directly call it. If you have written your class as student.
You can't directly override the clone method in it. You can't do this, there will be an error. We'll see what that error is and why. First of all, what is the error? The error is that any class can override this clone method only if it implements the interface called clonable. We have an interface called clonable in Java. If you implement it, meaning if your student class, class student, Implements.
Now, why this rule? We will see this in a while. First, let's go to the code and see how clonable is implemented. I told you, let's say, I have written equals here. We have already overrided hashCode We will try to override the clone method Now because that method is protected So I have to write protected here Now you can override protected Because you are in child class And you can access protected members in child class Now its return type is object And method name is clone There is one more gap here Protected object clone We will throw here Clone not supported exception Okay Here we have written public object clone. Throws clone not supported exception. Now I want to write the body of this method. Now I know that whoever will clone the object should clone it. I don't want to clone it by myself. So I write return super dot clone. So I said that whatever the clone functionality will be in the object, it will be implemented by super dot clone. Now see I haven't got any error yet. So with this method I can clone my object. Now how do I clone? First I remove all the print functions. I am making a new type of student. Let's say student. Student. S3.
Yes, s1. clone, you know, throws clone node supported exception. So here, this main function also has to handle that exception. We will see that later in exception handling. For now, let's assume that we have to put here too, throws clone node supported exception. Now see, there is no error in the code. So generally, I am expecting that everything will come in s3 which is in s1. Now if I want, I can print things from s3. But first, I will run the code and see if I am not getting any error. and error is coming, clone node supported exception why? because java wants that this class named student it is extending the object but it implements a clonable interface now when I do this now I will not get any error see no error came now if I want then I can print the details of s3 here something.
S1. clone, as I write, what happens? It clones, creates a new object in memory and points it with S3. Then I am trying to print its things on the screen. Okay. So this is our whole clone method. Now let's see why we have to implement clonable interface. Now see, we have to implement clonable interface. Before that, I made a mistake here.
Suppose you have made an object of a database or a thread. You can't clone them because the undesired output will come. So Java makes it mandatory that any class that you declare, let's say student, if you want to clone it, then implement a new interface. Implements clonable. we have to implement a clonable interface. This is the rule. So this means that Java will read it like this, any class that implements a clonable interface, I can clone that. So you explicitly told Java that I want to be cloned. So implement the clonable interface. Now you can write this clone method by overwriting super.clone. You can write anything in this. So now we also learned that if you implement an interface, then you have to overwrite all its methods. But we said clonable, we didn't overwrite any method. Do you know why we didn't? Because there is no method in clonable. So this interface, interface clonable, it looks like this. It opens and closes. There is no code written in it.
These are just for marking that if you implement this, then you will unlock some functionality. Like if you implement clonable, then only you will be able to clone. If you don't implement clonable, then you won't be able to clone. So it gave you a mark. So from inside, what code would Java have written of its own clone? So the Java's own clone class, where did we make it? Yes. So the default behavior of the Java's own clone class would be something like this So there would be some code written in it, let me talk about the studio code If whatever our object is, if this object implements.
Allow it Else throw clone not supported exception Okay, so here I wrote else throw our same exception clone not supported exception this one basically so some kind of code will be written from inside so java will not allow it it will check whether you have implemented the clonable interface or not we call these kind of interfaces as marker interfaces as soon as we get such interfaces we will see them but we call these as marker interfaces ok one more thing this java's inbuilt clonable function this default clonable function So the inbuilt clonable function of java always does shallow copy.
Now, Java used to use garbage collection to finalize memory. If you have stored something in your heap memory and it is not being used anymore, then garbage collection used to execute the finalized method before it runs. You could have called finalized by yourself. But Java said that this method is very unpredictable. We found out that it is very unpredictable, very unsafe, in fact, very unreliable. Why?
Even though, even if you call garbage collection yourself, it is not necessary that the finalized method will be called or not. It depends on garbage collection. When we will study garbage collection further, then you will understand that this method is Garbage collection is not in our hands. GEC decides whether this call will happen or not. So sometimes this call happens, sometimes it doesn't. That's why it's unpredictable, unsafe, unreliable. So there are many reasons. So it's been deprecated. Now no one uses it. So we can leave it. It's still in our object class for legacy. But its use is completely over.
Now all these methods are thread oriented methods And we will study this when we will understand multi-threading in depth in java Now you will understand this with threads only We have covered all the methods in object class 2 string equals hashcode get class We have seen clone too We have seen finalize is now deprecated And we will study threads later Then we will see it properly Now apart from this I want to tell you one more thing The arrays that we have in java are also non-primitive, that's why we declare them with new keyword int arr equal to new int 5 To make an array of 5 sizes So we know ARR is made in our stack memory And this new int5 The array of 5 sizes is made in our heap memory And ARR points it So internally If the arrays are non primitive Then our object class will also come above them Right which means we can do everything in object, we can do that in arrays as well and one more thing, I have written arrays here but in java arrays are different which is a data type and arrays class is a different class which is made for operations on arrays the way we will use it, we will see that slowly but you remember that in java many places the same names are used like I said, one is object class and one is the objects class.
Java.lang package, which is the parent of all. And objects is a utility class. Right? You are going to see this a lot in java. A class name changes by applying an s, but we will see it when it is used. But you should know that arrays is also a non-primitive data type. The object class also comes on top of that. So basically, the object class is the parent of every class. It comes on top of everyone, on the root level of everyone. So are these our primitive data types, like our int, char, Float.
So there is no object class above them. That's why we don't do any. to string,. equals etc. We have to compare them. There are two integers int a, int b. If I want to compare them, I will write a equal equal b. I don't write a.equals b. I can either use two strings or any other method of object. But we have wrapper classes like integer, character, float, and non-primitives. So what comes on top of them is our object class. Something like this. So, I hope you understood this well. What is object class? Why is it there? What are the methods in it? And their complete hierarchy.
