This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Superinterface?

 
Prakash Mani - Attur
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please anyone explain the below. i cant get what that mean. I know this is not a place to ask explanation for this passage but i have no other option. please help. I found this in langspec 3.0..

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.
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at java API doc for class Object, and look at all the public instance methods.

An interface, if it has no direct superinterfaces, implicitly has all those methods abstract.

For example.

In Object class has a public instance method:



Well every interface has a method like that



But it's a compile time error if you declare a method that in Object is final. For example you can't write an abstract method in your interface


or


since in class Object is declared final. So you would get the following compiler error

...I cannot override notify() in java.lang.Object; overridden method is final
public abstract void notify();
^
1 error


Now look at this example. It will cause a Runtime exception (NullPointerException) but it compiles fine



As you can see interface I has implicitly the method toString()
 
Mike Simmons
Ranch Hand
Posts: 3038
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicola Garofalo wrote:An interface, if it has no direct superinterfaces, implicitly has all those methods abstract.

From the thread title, I think it's the term "superinterface" that is causing confusion. It's defined here, but that may be less than clear to most people. So let's try again: for a class C, a superinterface is any interface that class C implements, either directly or indirectly. So if class C implements interface Foo, and interface Foo extends interface Bar, then both Foo and Bar are superinterfaces of class C. Even if Bar was not explicitly mentioned in the declaration of C. The superinterfaces of a class are all the interfaces implemented by that class. The superinterfaces of an interface are all the interfaces extended by that interface.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always say where your quote is from; which part of the Java™ Language Specification was it?
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prakash Attur wrote:Please anyone explain the below. i cant get what that mean. I know this is not a place to ask explanation for this passage but i have no other option. please help. I found this in langspec 3.0..

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.


This was super complicated...I've never seen it in use before, as in I've never seen any interface that makes calls to the object methods. Maybe there are some special use cases where this is useful?
 
Paul Clapham
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It happens all the time. Consider this code:

This implicitly calls the toString() method of rs, which is declared in Object. Note that without this paragraph, the interface ResultSet would not have a toString() method.
 
Prakash Mani - Attur
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes now i can understand what that passage mean.. thanks everyone especially to Nicola Garofalo ..
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:It happens all the time. Consider this code:

This implicitly calls the toString() method of rs, which is declared in Object. Note that without this paragraph, the interface ResultSet would not have a toString() method.


Paul, it is not like that interface is calling an object method , println method uses String.valueOf which uses object's toString method to display the interface implementation class

correct me, if i am wrong.
 
Mike Simmons
Ranch Hand
Posts: 3038
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you're correct. That particular code does not actually rely on the quoted rule to compile; it relies on other rules. (Like the fact that any interface has a widening conversion to Object, and Object has a toString().) However it's not difficult to write a version that does require the rule quoted above:
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wonderful discussion. thanks Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic