T E C h O C E A N H U B

Inheritance in depth(equals(), hashCode() and toString()) -(chapter-9)

In previous lecture, we learned about what is inheritance? but now in this lecture we will study in depth about methods involved in Any(which is superclass in kotlin). Any class in kotlin use to inherit these three methods from the base class (which is Any). Now let us study about these methods in details. Below is a basic image which shows method involved in Any class for inheritance.

Let us study about these method in detail.

equals():- This method is normally used in every language which involves the inheritance concept and can be defined as below syntax:-

                 open operator fun equals(other: Any?): Boolean

This above syntax is denoting a equals method structure in kotlin or to explain in simple way we can denote it like:-

                                emp1.equals(emp2)

In this case we are comparing one object with other objects contents hence overall it is just a comparison.

hashcode():-This method will return hash code value of the object, if two objects are equal according to the equals() method then calling hashcode() method on each of the two objects must produce the same integer result. The basic syntax of hashcode() is:-

                              open fun hashCode(): Int

The above is a basic syntax of hashcode in kotlin.

toString():-This method will convert kotlin data class object to string, this method can be overrided as other 2 methods can be. The default syntax of this method. The basic syntax of this method in kotlin is :

                                  fun toString():String

The above method will definitely return string as it return type signifies.

Feel free to read the post and comment below.

Below is my android application, and is useful for people who has Google Opinion Rewards to cash out. https://play.google.com/store/apps/details?id=com.manasvi.sawant.rewardtocash

If you wanted to create a website, please visit my fiverr gig link below or contact me on support@techoceanhub.com. https://www.fiverr.com/share/EdZ9L7

Copyright ©TechOceanhub All Rights Reserved.