• Post Reply Bookmark Topic Watch Topic
  • New Topic

query on interfaces and classes  RSS feed

 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface I {}
class unrelated{}
class Base{}
class sub extends Base{}
class sub1 extends Base implements I{}
public class C
{
Base b= new Base();
sub bro = new sub();
sub1 sisi = new sub1();
unrelated unrel = new unrelated();
I i;

public void test(){
i = (I)bro;
i= (I)sisi;


// unrel = (Base)b;
/*
need to know what allows Interface I to be cast into a sub (bro) especially as bro has no
relation wid Interface I .... thus i assume they do not have any common subclass
so ideally this attempt shud fail at compile time. Just like Base cannot be cast as
unrelated.
*/



sisi = (sub1)i;
bro = (sub)i;

}
public static void main(String[] s)
{
new C().test();
//sub ss = (sub)b;

}
}
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Well, the compiler doesn't know that. All it knows is that there is an object which supports the interface "I" that is requested to be casted to "sub" -- and interestingly, it doesn't know if there is another class such as this...



Unless "sub" is a final class, it doesn't know if there will be another subclass that will implement "I", and can be casted to "sub".

Henry
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i did not quite get how class bro (sub) quite tells the compiler that it supports the "I" interface ??
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sid sree:
Well i did not quite get how class bro (sub) quite tells the compiler that it supports the "I" interface ??


The sub class doesn't support the I interface, but a reference to sub can also hold subclasses of sub, including a sub2 object, which does implement the I interface.

Henry
 
sravani jetty
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,
Can you please elaborate how object of sub can be type casted to Interface i.

Thanks
Sravani
 
Ankith suresh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry
Does it mean a class ref can cast to any interface?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankith suresh:
Hi Henry
Does it mean a class ref can cast to any interface?


No. If the reference is of the class type that is final and does not implement the interface, then the compiler can determine that it is not possible to cast to that interface.

Henry
 
Akhilesh Trivedi
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks Sid and thanks Henry. This one is really tricky.
 
Deepak Vadgama
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer to the rules of Conversion,

http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#20232

Refer section 5.5, they have nicely explained it.....
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi henry

I guess there has been a small error while you where reading the code

"a reference to sub can also hold subclasses of sub, including a sub2 object"

No where in the code does sub extends sub1

Both sub & sub1 extends Base !!!

So please try to answer my query on the basis of this !!!

Thanks really appreciate you trying to help me out !!!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sid sree:
Hi henry

I guess there has been a small error while you where reading the code

"a reference to sub can also hold subclasses of sub, including a sub2 object"

No where in the code does sub extends sub1

Both sub & sub1 extends Base !!!

So please try to answer my query on the basis of this !!!

Thanks really appreciate you trying to help me out !!!


Sid,

I read your code fine. If you noticed, I didn't mention sub1, I mentioned sub2, which I gave the code for. The reason the cast is allowed is because of the possible existence of sub2, a class that subclasses sub and implements I.

Of course, you can say, but you don't have a sub2 class! Yes, that is true, but how does the compiler know that. What if this sub2 exist on the runtime platform, because of a different classpath? What if this sub2 will exist in the future?

Henry
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi henry

i got what you are trying to say .. probably should have got it on reading your previous posts itself ... but i guess better later than never ..... so in a nutshell any non-final class can be cast into any Interface irrespective of whether it implements it or not !!!

Please correct me if i,am wrong

Thanks a lot
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!