• Post Reply Bookmark Topic Watch Topic
  • New Topic

interface reference variable  RSS feed

 
Vipul Chauhan
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interface I{}
class Cls{}

Cls c = new Cls();
I i = (I) c; // no compilation error..

String s = new String();
I i 2= (I) s; // compilation error


why?
I and Cls has no relation still it compiles.


thanks.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question!

String is a class marked final. Try to make the Cls class final and see what happens...
 
Vipul Chauhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if class is not final that means it may have a child class which implements interface



thanks Roel De Nijs,
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If class Cls is not final, it can be extended and one of its child classes could implement interface I

Like in this code snippet:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!