| Author |
Superinterface?
|
Prakash Mani - Attur
Ranch Hand
Joined: Oct 08, 2009
Posts: 100
|
|
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
Joined: Apr 10, 2010
Posts: 308
|
|
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()
|
Bye,
Nicola
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2782
|
|
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
Joined: Oct 13, 2005
Posts: 32712
|
|
|
Please always say where your quote is from; which part of the Java™ Language Specification was it?
|
 |
Saifuddin Merchant
Ranch Hand
Joined: Feb 08, 2009
Posts: 576
|
|
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?
|
Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
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
Joined: Oct 08, 2009
Posts: 100
|
|
|
Yes now i can understand what that passage mean.. thanks everyone especially to Nicola Garofalo ..
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
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
Joined: Mar 05, 2008
Posts: 2782
|
|
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
Joined: Jan 28, 2008
Posts: 5575
|
|
wonderful discussion. thanks Mike
|
 |
 |
|
|
subject: Superinterface?
|
|
|