I think the answer should be A & D. It doesnt matter what order the interface is declared in.It could even be in another file. Order does matter with variables in a class. For example you couldnt say this: ========= int x = b + c; int b = 1; int c = 3; ========== The casts on both line 4 and 5 are mandatory.On 4 because A doesnt directly implement E. Try E e = new B(); -> This is fine because B directly implements the interface E. But at run time a really is a D so no runtime error. If a was an A = runtime error.
Option A is correct : No compile or runtime errors come out.
Options B and C is not correct due to above.
Option D is correct : Although (a) contains a reference to class type D, the compiler doesn't know that. The compiler sees (a) as type of class A, so the cast is mandatory here in otder to allow the code compile.
Option E is incorrect: Same explanation as above.
"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
I dont get why the answer is not C...the rules of object reference casting in Philip Heller/Simon Roberts book says,"when castin one class to another or one interface to a class,one of them should be a subclass or implement the other in order for the code to compile ".Here in the example i dont see a chance of it compiling 'cos A does not implement E but has been cast to E,which is wrong.Anyone who is really sure abt this concept,pls help.
--------------- The detailed rules for compile-time correctness checking of a casting conversion of a value of compile-time reference type S (source) to a compile-time reference type T (target) are as follows:
If S is a class type:
If T is an interface type: If S is not a final class (�8.1.1), then the cast is always correct at compile time (because even if S does not implement T, a subclass of S might). ---------------
In the sample code posted by Ganesh, class A is the S and interface E is the T. Since class A is not final, it can be extended and its subclasses may implement E.
Example with class A being declared as final:
Example with E as a class:
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com