• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I wonder how this happened

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 . . .
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you check what is the class Object and its purpose?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mina Daoud wrote: . . . It wasn't reply to your post . . .

Sorry. I misunderstood that.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rakesh kadulkar wrote: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.



To all,

If hashCode() method is in interface than what about other methods of the Object class like if you call ref.getClass()

And also ref.hashCode() method is also not defined in the top level interface implicitly at compile-time. Because after compilation as you can see their is no hashCode() method in interface A when you run        javap A command.

Than why compiler is not giving compile-time error.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Because all objects inherit (often indirectly) from Object, whenever you declare a reference of type MyInterface, it is implicitly of type Object too. So the eleven methods of Object are implicitly inherited by every object irrespective of how it is declared.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohit Singhi wrote:. . . after compilation as you can see their is no hashCode() method in interface A when you run javap A command. . . .

The javap command only shows code declared in the current class. It doesn't show anything inherited unchanged from Object.
 
reply
    Bookmark Topic Watch Topic
  • New Topic