0

I have a class Dog extends Animal.

Then I call the hashCode() method as follows.

Animal animal  = new Dog(200);
System.out.println(animal.hashCode());

Here,
If I've overridden the hashCode() in the Dog class it will be returned. Else if I've overridden the hashCode() in the Dog class it will be returned. Else some integer returned.

I wanna know...

  • Why does it call the hashCode() of the super class when it is not overriden in the Dog class? How and what the 'some integer' generated

  • When the hashCode is not generated in anywhere. (I have heard that it is the memory location of the object but not sure.)

4 Answers 4

4

This is referred to as method overriding. The hashCode method is defined in java.lang.Object, basically the top of the object heirarchy, so it is always available to any Object defined in Java. If the method is not overriden in your specific subclass or one of its parents, the default behavior defined in java.lang.Object will be invoked.

You typically should not worry about what the internal implementation of a hash code is in a parent object, but the default implementation does use the internal address of the Object. Note that this internal address is precisely that - an interpreted address that the JVM uses internally, that should not be depended upon by an application to be anything particularly meaningful.

You can read more about how overriding works in the Java Language Specification - Section 8.4.8.

Sign up to request clarification or add additional context in comments.

6 Comments

Ah what you say is if the hashCode() is overridden in somewhere up in the class hierarchy, it will be called. Else, the java.lang.Object's one will be called. OK. Then what is this 'internal address'? is it a memory address or how it is generated? Thanx for the answer.
Saying it's "the internal address" is misleading. The value can be based on some address but it is not an address of any kind from which you can get useful information. It's simply a quasi-random number that (almost) uniquely identifies the object. (Both the randomness and the uniqueness are not guaranteed, however).
@JoachimSauer - I use the same terminology as Oracle uses in their Javadoc. I don't see anywhere in my answer where I imply the address has meaningful context outside the internals of the JVM.
@Perception: I'm not saying that it's wrong, I'm saying it's misleading. I've seen plenty of beginners take that phrasing far to literal.
To avoid any possibility of confusion, I've added a caveat about the default hash code.
|
0

In java every class, if not explitcitly mentioned, will have Object class as its parent class. As Object class defines the hashcode method this is avilable even if you don't define in your class. And yes, in java the default hashcode implemention is to return the memory location of the object. In a way this looks correct way as if two object are at same memory location than they must be same.

Comments

0

hasCode() is form parent class object so if you haven't override it then parent method will be called. I think what you refer to as some integer is the hashCode generated at the super parent Object. Which suggest you haven't override the hashCode in Animal class.

In general if a super class method is not overridden then the immediate parent's method will be called.

1 Comment

Thanx isuru, It's ok for me. But my major problem is how the default hashCode is generated and what is it?
0
  1. First In java every class, if not explitcitly mentioned, will have Object class as its parent class
  2. Java doesn't generate hashCode(), i.e. This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language. Most classes (especially if you are going to use it in any of the Collection API) specially in hashcontainer(HashSet and HashMap) should implement their own HashCode (and by contract their own equals method).

If you are interested to know when to implement hashCode() and equals() you can visit this site http://www.javabeat.net/2007/08/hashcode-and-equals-methods/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.