views:

82

answers:

6

Please explain me below situation What would be the output?


interface A{}
class B implements A{}
class C extends B{}
class D extends C{}
class E extends D{
public static void main(String args[]){
C c = new C();
B b = c;
A a = (E)c;
a = (B)c;
c = (C)(B)c;
}
}

A: 

What can class B be casted to? What can class C be casted to? What about D and E?

Michael Angstadt
i think you can deduce that from the inheritance...
ultrajohn
A: 

I think there's no output. Because you didn't tell it where the entry point is.

If you did specify the main class as D E, there would still be no output since all those classes extend an empty class.

Christian Sciberras
The main class is `E`
OscarRyz
+1  A: 

ClassCastException at A a = (E)c;

fede
yes but why? Can you please explain me this?
Abhishek Jain
even if the container "a" is of type "A", the cast is executed, but it is illegal since "c" contains an object of type C: since E is a specialization of C, c cannot be cast to E.
fede
Because the type of the object 'c' is C. It can't be cast to E.
Mike Baranczak
+1  A: 

Without actually trying it, I'll go out on a limb and say that this line will cause two compiler errors:

C c = (C)(B)c;
  1. You're declaring the variable 'c' twice.
  2. You can't cast from B to C.

And if you actually put a double cast like that into a real project, then you deserve to get your ass kicked.

Mike Baranczak
OK, I see that you've edited the code so that 1 doesn't apply anymore. And I was also wrong about 2.
Mike Baranczak
I have already corrected the mistake of declaring variable c twice. Thanks.
Abhishek Jain
+1  A: 

The object c is created as a new C. Since C extends B it is no problem assigning it to a variable of type B. However C knows nothing about E, so you can't cast here. You can only do this with super-classes. Since A is the absolute top level it is fine to assign any objects of the types you have defined to it.

DaveJohnston
+4  A: 

Being completely strict, that' won't compile because in line 4 you type Class instead of class

Class D extends C{}

And later you define twice a and c

C c = new C(); // once
B b = c;
A a = (E)c;    // once a
A a = (B)c;    // twice c
C c = (C)(B)c; // twice

Now assuming those were typos the output would be ClassCastException because c can't be casted to E.

When you perform a cast is like you were saying: "I'm the programmer and I know this is a..." ____(put your class here) And the compiler will allow you compile.

But if in runtime the instance is not really a __ ( an E in this case, which is not ) then it will throw ClassCastException.

The program won't fail with A a = ( B ) c; because c is an instance of C which is a subclass of B.

You can say that, C is a B. To understand it better think on the following declaration:

class Employee extends Object {
}

Every Employee is an Object so the cast will succeed, actually is it so clear that it will succeed that you don't even need to put the cast operator ().

 Employee e = new Employee();
 Object o = ( Object ) e; // or much better:
 Object o2 = e; // no cast needed when assigning to superclass. 

But not necessarily an Object is an Employee.

 Object o = ....
 Employee e = ( Employee ) o; // will fail if o was not created as an Employee. 

That's why A a = ( E ) c; fail, because, the reference c was not created as an E

I hope that helps.

OscarRyz
I have already corrected my mistakes
Abhishek Jain