aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interface to class assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interface to class assignment" Watch "interface to class assignment" New topic
Author

interface to class assignment

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
//Question :
//By commenting out which line of code can you make the class
//ZZY compile with no - compile time errors ?Consider that the
//following lines of code exist in a file called ZZY .java

interface XTC { }

class ZZY implements XTC {

public static void main ( String ka [ ] ) { // ---> line 1
ZZY z = new ZZY ( ) ; // ---> line 2
XTC x = z ; // line 3
z = x ; // ---> line 4
Object o = x ; // ---> line 5
}
};

/*
Options :

a . line 1
b . line 2
c . line 3
d . line 4
e . line 5
*/

as per me, answer is d. and its correct too as per the voodoo
exam software. But if the class=interface; assignment
on line 4 doesnt work without an explicit cast, then
hoe come the class=interface; assignment on line 5 work ?


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Mike Noel
Ranch Hand

Joined: Dec 15, 2005
Posts: 108
It's not a matter of casting between interfaces and classes. Lines 3 and 5 are both instances of upcasting (assigning a subclass object to a super class reference) while line 4 is an example of downcasting (assigning a parent class object to a subclass reference). Downcasting requires an explicit cast in order to compile.

You would see the exact same behavior in your example if you changed XTC to a class and made class ZZY extend XTC.

_M_


Mike Noel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface to class assignment