Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

JLS 6.4.3 The Members of an Interface Type

 
Ranch Hand
Posts: 4702
9
Scala Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
6.4.3 The Members of an Interface Type
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.
two things i find interesting about this are:
1) a non-abstract member can be overridden to be abstract.
2) since any class implementing the interface inherits from Object anyway, whats the point?
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also agree that there doesnt appear any reason for interface to declare all methods defined in the object class.
My guess is that this is being done for bytecode compatibility or some obscure reason like that. I would like to know about it.Does it play a role in convertion of Interface variables to Objects
[This message has been edited by mohit joshi (edited December 08, 2000).]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Does it play a role in convertion Interface variables to Objects?

Probably because how else would objects of type Serializable (for example) be stored in a Vector (which can only take Objects).
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following:
String s = "";
Object o = s;
Why is this possible? Because a String ISA Object, in the sense
that String inherits all methods of Object.
Now consider this:
Serializable s = ...;
Object o = s;
This to possible only if a Serializable ISA Object. For this to happen, Serializable has to have all methods that an Object have. Hence, 6.4.3 of JLS.
Hope this helps,
Panagiotis.

Originally posted by Randall Twede:
6.4.3 The Members of an Interface Type
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.
two things i find interesting about this are:
1) a non-abstract member can be overridden to be abstract.
2) since any class implementing the interface inherits from Object anyway, whats the point?


 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does it mean ISA?
I always been intrigued by declarations like
Map variableName;
Serializable otherVariable;
Those variables gonna represent some objects implementing interfases, not classes, whether it possible to initialize them , and if it's positive - how?
Hopefully not much offtopic.
[This message has been edited by Vladimir Kositsky (edited December 08, 2000).]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thomas and Panagiotis, I agree with you... But then, the actual statement is that
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.
But, what's the big deal in making them all abstract? Doesn't it mean to say that a class implementing an interface with no direct superinterface to compulsarily implement that abstract method?? Otherwise, it shud result in a compilation error, which doesn't happen... then, what's the meaning of having an implicit abstract method in such an interface?
Thanks,
Aparna
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

An interface implicitly defines all the methods of Object.
Hence if you have an empty interface like this
interface IUnknown{ }
you can create a non abstract class like this
class Base implements IUnknown{}
It is not necessary for Base to be abstract as
Base implicitly extends Object. And all the abstract methods
that IUnknown has defined have been implemented in Object.
There are no restrictions on overriding a concrete method to abstract again, as demonstrated by this example.
<pre>
interface IFoo{
public void setVal();
public long getVal();
}
class Base implements IFoo{
public void setVal(){}
public long getVal(){ return 0; }
}
abstract class Derived extends Base{
abstract public void setVal();
}
public class Test extends Base{
public void setVal()
{
System.out.println("Setting Value");
}
public static void main(String[] args)
{
Test t = new Test();
t.setVal();
}
}
</pre>

Rgds
Sahir
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aparna,
(0) The methods have to be there because otherwise the interface won't polymorphically be an Object.
(1) The methods *have* to be public abstract because an interface can only contain public abstract methods. It can contain no implementation.
(2) Say that a class C implements an interface I that has no direct superinterface. Yes, it is compulsory for C to implement all abstract methods of I that correspond to the methods of Object. But no special effort need be done for this since C is a subclass of Object anyway. So, the schematic would look like this:
<pre>
Object
^ ^
| \
| \
C --> I
</pre>
Hope this helps,
Panagiotis.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I agree with Panagiotis.
Any and every object in java is derived from class Object.
With class it achieves this by the rule that any class is child of class Object.
With interface with no methods it does by creating abstract method corresponding to the one in Object.
So interface with no method implicitly declares abstract methods corresponding to each method in Java.
But then the question is why it only does for the interface which has no method and why not with interface declared with abstract method ?
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kartik,


You ask:
"why it only does for the interface which has no method and why not with interface declared with abstract method ?"



This is not true. It does not matter whether the interface (with no direct superinterfaces) has 0, 1, 2, or N member methods.
The public abstract methods implicitly declared to make the interface polymorphically an Object are in addition to any
methods the interface itself declares.
Just read $6.4.3 of JLS at the point were it says "The members of an interface are: ..."


Cheers,
Panagiotis.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!