• 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

casting problem.

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. public class Test{
2. public static void main(String args[]){
3.class Foo{
4.public int i =3;}
5.object o = (Object)new Foo();
6.Foo foo =(Foo)o;
7.System.out.println("i = "+foo.i);
}
}
option:
i = 3;
compilation fail
classcast exception at line5
classcast exception at line6

ans is i=3; but as object is superclass and foo is subclass so can we cast superclass ref type to a subclass?
so i thought ans is 4h option ....
can anyone please explain?
[ January 30, 2006: Message edited by: swapnil paranjape ]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the code does not compile at line 5, its Object not object
 
swapnil paranjape
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry it was typing mistake..its Object in both the lines..so can you please explain me the ans?
 
ven kaar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object o is actually holding the reference of Foo, new Foo(), so there is no issues, it will compile and run
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. public class Test{
2. public static void main(String args[]){
3.class Foo{
4.public int i =3;}
5.Object o = (Object)new Foo();
6.Foo foo =(Foo)o;
7.System.out.println("i = "+foo.i);
}
}
this will print correctly. at line 5 no need of explicitty cast.
because of widening means a super class can refer to a subclass.
after line 5 o represnts object of class at foo at runtime.
casting check both compile time and runtime. at compile time
both the reference are checked.(here Foo and Object).they have
super-sub relation. so compiles. at runtime o represents a object of class foo coz of line 5. so casting is successfully. so foo.i=3.
for more refence read khalid mughal ch6.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
swapnil paranjape,

Here, in line 5, we create an object of class Foo. It's created in the heap and it has the attribute i which is initialized to 3. then in the same line we ask JVM to cast that Foo object to a Object type instance. Casting doesnt mean that we loose any information in that casted object. Casting means that we just ask the JVM to treat the object as casted type. So, in line 4, because of created a Foo object, it will remain same eventhough we cast to Object type. only difference it, we cannot access variable i. In line 6, we cast it back to Foo type. then again, we ask JVM to treat the object as a Foo object. because it was initially a Foo object, it will retrieve the earlier setted atributes. that means , initialy i was 3 and when casting back, it retrieve the earlier value. Hope you got what i said.
[ January 31, 2006: Message edited by: Roshan Amadoru ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A super class reference can hold any of the sub-class objects without any type casts, When you do so you can access only the methods that are defined in the super class, Any attempt to access the sub-class method will result in compilation errot, However if you overrided a method in sub-class and when you call the method (Essentially both have the same name) It will run and finally the method in the child class is called.{Late binding}.

However when you want to assign a super-class reference to a child class you will have to explicitly type caste it to your child class, Or else the compiler will complain.

Now,
P p = new C1();
C2 c = (C2)p;
If the super class refernce is refencing to some other child class (C1) then you will get a class caste exception, But the complier wont complain.
You can avoid this class caste exception if you use isntanceof
if(p instanceof c)
C2 c = (C2)p;
So now this condition will fail and you wont get any exception,
Bye
deepu
 
Would you like to try a free sample? Today we are featuring tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic