• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Doubts on downCasting in Java.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doubts posted below code snippet:

class A {}
class B extends A{}

public class MyClass
{
public static void main(String args[]){
A a = new A();
B b = new B();

a = b ; // 1.... typical upcasting
b =(B)a; // 2.. typical downcasting

a = new A(); //3...assigning new reference of Type A
b =(B)a; // 4...compiles but throws CCE at runtime
}

}

i> Here at 1 we assign reference of type B to a reference of Type A. Hence the Type A object reference (a) now points to an address which contains an object of type B. At 2 we are going reverse. Now, after executing step 1 why does the compiler require an explicit cast at 2 ?

ii> At 3 we are creating a new instance of A and assigning it to a B type reference at 4. While the compiler doesn't complain at 4 but the runtime throws up a ClassCastExcp. How can the compiler allow something which the runtime flatly rejects?

iii> Why should I use downcasting in any scenario since in the end game I am only getting a reference to a Type B object(same or different) using a Type B object? Since through inheritence I am already accessing all the members of my superclass I donot need to use downcasting at all. Please highlight any scenario where we cannot do w/o downcasting at all.


Thanks in Advance for sharing your valuable time. I have made the doubts as legible and intelligible as possible.
 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please find below my attempt to answer your questions.

1. A Child is derived from Parent hence explicit cast is required

2. As Parent is not derived from Child, ClassCastException is thrown

3. Answers to questions 1 and 2 answer 3
 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samwise Gamedee wrote:Doubts posted below code snippet:

class A {}
class B extends A{}

public class MyClass
{
public static void main(String args[]){
A a = new A();
B b = new B();

a = b ; // 1.... typical upcasting
b =(B)a; // 2.. typical downcasting

a = new A(); //3...assigning new reference of Type A
b =(B)a; // 4...compiles but throws CCE at runtime
}

}

i> Here at 1 we assign reference of type B to a reference of Type A. Hence the Type A object reference (a) now points to an address which contains an object of type B. At 2 we are going reverse. Now, after executing step 1 why does the compiler require an explicit cast at 2 ?

ii> At 3 we are creating a new instance of A and assigning it to a B type reference at 4. While the compiler doesn't complain at 4 but the runtime throws up a ClassCastExcp. How can the compiler allow something which the runtime flatly rejects?

iii> Why should I use downcasting in any scenario since in the end game I am only getting a reference to a Type B object(same or different) using a Type B object? Since through inheritence I am already accessing all the members of my superclass I donot need to use downcasting at all. Please highlight any scenario where we cannot do w/o downcasting at all.



Think about is as boxes. Box A is 10x10 cm, standard cardboard box, suitable for dry things. Box B is 20x20 cm and plastic and may hold liquids.

i: You can always take a larger box B and state that is can old at least the amount of things as a smaller A-box and hold anything. You have to check if the box you got in your hand is 20x20 and plastic (explicit cast) to make sure that you can promise that the box can hold 400 cc of liquid.

ii: All casts are done in runtime since we need an instance to operate. In compile time, no instances exists.

iii: You should always strive to have as loose coupling between two classes as possible. Say i.e. that you have a method that handles Boxes. If you ask 5 different Box-factories to "Give me all your Boxes" you don't want to get back ArrayList<BigBox> from one, LinkedList<SmallBox> from another and Stack<PizzaBox> from the third and so on. Instead, what you want is to make them all return something that can be handled as a List<Box> so that you from the Box can get the attributes you need to handle them.
 
Samwise Gamedee
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ove, Some good examples to support your theory.
 
reply
    Bookmark Topic Watch Topic
  • New Topic