File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock Exam Question - Instantiation and casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock Exam Question - Instantiation and casting" Watch "Mock Exam Question - Instantiation and casting" New topic
Author

Mock Exam Question - Instantiation and casting

Ann Sebastian
Ranch Hand

Joined: Sep 21, 2005
Posts: 37
This is from Valentin Crettaz Mock Exam



A. The code compiles without error and runs fine.

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
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
E e;
First, can we create objects of interfaces?? Leave the assigning part what the above statement will do?


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Lakshmanan Arunachalam
Ranch Hand

Joined: Nov 02, 2005
Posts: 99

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.


Regards<br />Lakshmanan<br />IBM-OOAD & UML, SCEA-I
Cheenu Subramanian
Ranch Hand

Joined: Aug 15, 2005
Posts: 40
E e;
implies a interface reference that can point to the objects that implement that particular interface.

It does not create an interface object and as you know by now that we cannt instantiate an interface
 
Don't get me started about those stupid light bulbs.
 
subject: Mock Exam Question - Instantiation and casting