Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Kotlin get default hashCode implementation for inherited class

Before overriding, each class in Kotlin/JVM has default equals/hashCode implementation. Equals is checking for reference equality with ===, but hashCode is something else (and I don’t know what it is).

I wonder if I can get the default implementation of hashCode back once overriden, consider this example:

open class A {
    override fun hashCode(): Int = 1
}

class B : A() {
    override fun hashCode(): Int {
        // how can I get the default implementation here?..
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can’t access to super.super methods, but the JVM actually provides a method to get the original hash code value:

class B : A() {
    override fun hashCode(): Int = System.identityHashCode(this)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading