• Post Reply Bookmark Topic Watch Topic
  • New Topic

interface reference variable

 
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: 10763
148
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: 10763
148
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:
 
I want my playground back. Here, I'll give you this tiny ad for it:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!