Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

query on interfaces and classes

 
Ranch Hand
Posts: 188
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;

}
}
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,
Can you please elaborate how object of sub can be type casted to Interface i.

Thanks
Sravani
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry
Does it mean a class ref can cast to any interface?
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanks Sid and thanks Henry. This one is really tricky.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I want my playground back. Here, I'll give you this tiny ad for it:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic