Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes I wonder how this happened Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "I wonder how this happened" Watch "I wonder how this happened" New topic
Author

I wonder how this happened

rakesh kadulkar
Ranch Hand

Joined: Jul 24, 2008
Posts: 115
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?


Rakesh Kadulkar
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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

Joined: Jul 24, 2008
Posts: 115


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

Joined: Apr 06, 2010
Posts: 4372
    
    8

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

Joined: Oct 27, 2005
Posts: 19680
    
  19

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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

Joined: Sep 24, 2010
Posts: 88
Can you check what is the class Object and its purpose?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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

Joined: Sep 24, 2010
Posts: 88
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

Joined: Jul 24, 2008
Posts: 115
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

Joined: Oct 13, 2005
Posts: 38481
    
  23
Mina Daoud wrote: . . . It wasn't reply to your post . . .
Sorry. I misunderstood that.
 
 
subject: I wonder how this happened