Win a copy of Head First Android this week in the Android 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Casting problem

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gooday Everyone.

I really need your great help, if anyone could please help me to explain the below line. I have tried very hard to understand, but in vain. Maybe my some concepts are wrong. Please help me. This is from Examlab Practise test1 Question 29

1. interface Min{}
2.
3. class A{}
4.
5. class B extends A implements Min{}
6.
7. class C extends B{}
8.
9. public class CCExp{
10. public static void main(String args[]){
11. A a=new A();
12. B b=new B();
13. C c=new C();
14. Min m=null;
15. b=(C)m;
16. m=(Min)b;
17. A a2=(A)m;
18. B b2=(B)a2;--------the reference type of a2 is A, then we cannot cast A to B, it will give class cast exception. But the answer is that it
compiles and excutes. Please can anyone explain how is this possible.
19. Min m2=(C)b2;
20. C c2=(C)m2;
21. }
22. }
 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mushraf Khan wrote:Gooday Everyone.

I really need your great help, if anyone could please help me to explain the below line. I have tried very hard to understand, but in vain. Maybe my some concepts are wrong. Please help me. This is from Examlab Practise test1 Question 29

1. interface Min{}
2.
3. class A{}
4.
5. class B extends A implements Min{}
6.
7. class C extends B{}
8.
9. public class CCExp{
10. public static void main(String args[]){
11. A a=new A();
12. B b=new B();
13. C c=new C();
14. Min m=null;
15. b=(C)m;
16. m=(Min)b;
17. A a2=(A)m;
18. B b2=(B)a2;--------the reference type of a2 is A, then we cannot cast A to B, it will give class cast exception. But the answer is that it
compiles and excutes. Please can anyone explain how is this possible.
19. Min m2=(C)b2;
20. C c2=(C)m2;
21. }
22. }


Although the reference type of a2 is A but it is referring towards null which can no doubt be referred by reference of B. So this will compile and run fine. It would have been giving Class cast exception if a2 would have been referring object of A.
 
Mushraf Khan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Astha. That was really easy to understand, had this doubt for last 15 days . Thanks a lot.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Casting cannot be done only if the type being casted is not it's parent.

 
Mushraf Khan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot John. Example was good understanding to clear the doubts
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic