Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I wonder how this happened

 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Some time ago I was doing some RnD on web Sessions using HttpSession.

I observed that if you have an interface A for example.

Class B implements ineterface A. and interface A is empty, it has nothing in it.

The statement


Does not gave me compile time error and it ran perfectly. Again interface A is empty.

How is this possible?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this.

What have you done? You have declared a variable, assigned an object to it, and called its hashCode method. Write down the inheritance hierarchy of ref, and it should become obvious to you where the hashCode() method comes from. If that doesn't work, try . . . and that should give a hint where the hashCode() method comes from.
 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output : 1476323068

See "Inter" interface does not have hashCode() method and if you see the "ref" static data type is "Inter".

So the rule says that using interface reference you can call only those methods on the target object which are there in the interface.

Now i think I have made my point clear
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Inter can only be instantiated as a concrete class, and a concrete class will always inherit from Object. Since there is no possible way the call to hashCode() can be invalid, there's no reason for the compiler to see it as an error.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From section 9.2 of the JLS:
If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type "r", and throws clause t corresponding to each public instance method m with signature s, return type "r", and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.

In other words, every "root" interface implicitly has the following methods:
- public abstract boolean equals(Object)
- public abstract int hashCode()
- public abstract String toString()
- likewise for the final methods of Object (getClass(), notify(), notifyAll(), wait(), wait(long), wait(long, int)
(note that clone() and finalize() are not implicitly declared; these are protected in java.lang.Object and all methods in interfaces must be public)

These last methods cannot be explicitly redeclared, the first three can.

The reason is because of what Matthew said: in the end, no matter what the concrete class of the actual instance, Object is at the root of the hierarchy.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought it would have been obvious that every instance of every class inherits from java.lang.Object, but maybe it isn't so obvious . . .
 
Mina Daoud
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you check what is the class Object and its purpose?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mina Daoud wrote:Can you check what is the class Object and its purpose?
You can do that for yourself in the API documentation.
 
Mina Daoud
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mina Daoud wrote:Can you check what is the class Object and its purpose?
You can do that for yourself in the API documentation.


It wasn't reply to your post, its for the main question in the topic
 
rakesh kadulkar
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To all,

I know that the hashCode() method is coming from the Object class because while programming in java Object calss is inevitable.

My question was

"If I have a reference of type interface which does not have a hashCode() method in it then how come I can call hashCode() method on the target object using that interface reference"

With some of the reply I understood that if it is a top level interface then it has by default hashCode() method in it.

Otherwise there is no magic happening here.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mina Daoud wrote: . . . It wasn't reply to your post . . .
Sorry. I misunderstood that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic