• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with cast

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, there is my code :
interface ssss {}
class c1 implements ssss {}
class c2 implements ssss {}
public class Test {
public static void main (String w[]){
c1 oc1= new c1();
c2 oc2= new c2();

ssss ioc1= oc1; //ok
ssss ioc2= oc2; //ok
System.out.println(ioc1.getClass() + " - " + ioc2.getClass());

ioc1=ioc2; //?
}
}
ioc1 is of type c1 while ioc2 is of type c2 , my question is why there is not error (invalid cast ..) at run time ?

Thanks Gabriele Rigamonti
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ioc1 and ioc2 are of type ssss, BUT ioc1 is a reference to an object of class c1 and ioc2 is a reference to an object of class c2.
Since ioc1 and ioc2 are of the same type there is no problem assigning one to another
Val
 
reply
    Bookmark Topic Watch Topic
  • New Topic