File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting and Interfacses 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 "Casting and Interfacses" Watch "Casting and Interfacses" New topic

Casting and Interfacses

Ganesh Pujar
Ranch Hand

Joined: Mar 22, 2006
Posts: 45
Hi all

I'm not able to understand it
Plz explane me...

Rajesh MadhanGopal
Ranch Hand

Joined: Jan 19, 2006
Posts: 70
I think its option 1 , correct me if am wrong.

Thanks<br />Rajesh
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
Hi Ganesh,

The ans B is wrong as forward referencing is not in java this code will compile fine the ans should be A) and D).

Tamara Lopez
Ranch Hand

Joined: Feb 21, 2006
Posts: 63
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.
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Guys,

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
Vidhya Hari

Joined: Mar 20, 2006
Posts: 20
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.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Vidhya,

I dont get why the answer is not C...

Quote from JLS2 - 5.5:

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.

    For example:

    Example with class A being declared as final:

    Example with E as a class:

    I agree. Here's the link:
    subject: Casting and Interfacses
    It's not a secret anymore!