This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
B. Compilation error on line 1 because interface E is not yet declared (forward-referencing). C. Compilation error on line 4 because class A does not implement interface E.
D. The cast on line 4 is mandatory.
E. The cast on line 5 is not mandatory.
Answer is A and D
Here is the eplanation provided ...
First, pay attention to the class hierarchy (B and C are sibling classes!!) Then, there is no such thing as forward-referencing issues when using interfaces declared later in the compilation unit. On line 4, we are dealing with an object whose runtime type is D which implements interface E. The cast is mandatory, though, since the reference type (A) is not assignment compatible with the reference type E. The cast on line 5 is mandatory for the same reasons.
Could some body help me with this... How can line 4 compile Interface assigned to class
How can line 4 compile Interface assigned to class
Because, interface is a type, class is a type. Type casting is allowed in java between compatible types. if the interface and class are not type compatible, compiler can tell you those are not compatible.
can we create objects of interfaces??
Interface doesnt have constructors, we cant create objects for interfaces. Only we can assign.