• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does 'Object' type act as a supertype of all interface types in Java?  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the statement marked (1) in the following code is valid, whereas that of stmt (2) give complie-time error;

Is there a 'Supertype-Subtype' relationship between the 'Object' class & all 'interfaces types' in java?(i.e; Is 'Object' type, a supertype of all interface types in Java?) If YES, then this implies that 'interface type' can become a subtype of a 'class type' in Java. But then the following type of interface declaration gives error;

If NO, then what is the reason behind successful execution of stmt (1), while that of error in case of stmt (2).
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You can find out by doing a bit of compiling.
javac Foo.java Bar.java
javap -c Foo
javap -c Bar
You will see Foo has “java.lang.Object”.<init>; early in the printout, and Bar doesn’t.

But all classes which implement Bar do extend Object.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot declare a reference of type C and assign a B to it. You can sayorYou can’t say
 
Nayanish Patil
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, I agree that 'Object' type is not a supertype of an 'interface' type but I still cannot able to understand, how the following statement compiled successfully in the code;

I mean if there is no any relationship (such as the 'supertype-subtype relationship' between class 'Animal' and classes 'Cat' and 'Dog' from your suggessted example) between an 'Object type' & an 'interface' type then how can we assign an 'interface type' reference to an 'Object' type reference.

I think there might be some bonding or relationship between an 'Object type' and an 'interface type' due to which we can assign the reference of an 'interface type' directly to the reference of an 'Object' type.
Would you please clear this confusion?.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nayanish patil wrote:how can we assign an 'interface type' reference to an 'Object' type reference.


But you aren't doing that. The reference you are assigning is a reference to an object of some class which implements that interface. And therefore it's a reference to an object of some class which descends from Object, because (trivially) all classes descend from Object.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nayanish patil wrote:I mean if there is no any relationship (such as the 'supertype-subtype relationship' between...
Would you please clear this confusion?.

Dunno, but I'll try.

An interface is abstract - by definition - therefore, you cannot create instances of an interface; and it's only objects that extend Object.

Make any better sense now?

Winston
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's put it another way. The error you get when you do C cobj = iA is a type mismatch. This means that the compiler can't be sure that everything that could be assigned to iA could also be assigned to C. There could be some instances that are of the interface type but not of the class type. On the other hand, you don't get that exception when you do Object o = iA. This is because every object by definition is an Object (instance of the Object class). Therefore, by definition every object which can be referenced by iA can also be assigned to Object o and that assignment is safe.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nayanish patil wrote: . . . how the following statement compiled successfully in the code . . .
I think Paul C and Winston and Steve L have answered your question better than I could have, already (Thank you.)
 
Nayanish Patil
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS to all of you guys.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!