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.

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.