• 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

Superinterface?

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always say where your quote is from; which part of the Java™ Language Specification was it?
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes now i can understand what that passage mean.. thanks everyone especially to Nicola Garofalo ..
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wonderful discussion. thanks Mike
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic