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 type conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "type conversion" Watch "type conversion" New topic
Author

type conversion

sun moon
Greenhorn

Joined: Oct 09, 2002
Posts: 28
hi,
in java specification1.2 section 5.2,
one of the rules in conversion are.
- If S is an interface type:
If T is a class type, then T must be Object, or a compile-time error occurs.

i dint understand this point.
can u pls help me out using example
thanks
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
This rule is used when you have code like:


SCJP 1.4
sun moon
Greenhorn

Joined: Oct 09, 2002
Posts: 28
hi,
thanks,
that means left side it can only accept the "Class Object" no other object like show below can be accepted
interface Best{
}
class Rest implements Best{
}
class Test extends Rest{
public static void main(String args[]) {
Test r = new Test();
Best b = new Rest();
r = b; // note
}
}
note here r should be substituted by "Class Object" only
We can not use anyother object on the left side
is so
Alfred Kemety
Ranch Hand

Joined: Aug 14, 2002
Posts: 279
That's only with no explicit casting... with explicit casting, there shouldn't be this restriction, actually at compile time, trying to explicitly cast references of interfaces types to other references of classes types, the compiler is a lot less restrictive than trying to explicitly cast references of classes types to other references of classes types.
This compiles fine, but throws a run-time exception.

This compiles and runs fine, although Test doesn't actually implement interface X, YET it throws a run-time exception:
java.lang.ClassCastException: Y
at Test.main(Test.java:8)
Exception in thread "main"
Tool completed with exit code 1


Alfred Raouf - Egypt - SCJP 1.4<br />Kemety.equals(Egyptian) // returns true
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

"sun moon"-
Welcome to the JavaRanch! Please adjust your displayed name to match the JavaRanch Naming Policy. Specifically, it should not be obviously fictitious.
You can change it here.
Thanks! and again welcome to the JavaRanch!
This is your 3rd warning (1st, 2nd), please change your display name, otherwise your account will be disabled.
[ November 17, 2002: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Karin Paola Illuminate
Ranch Hand

Joined: Oct 18, 2002
Posts: 109
From "A Programmer's Guide to Java (Khalid A. Mughal, Rolf W. Rasmussen)" :
SourceType srcRef;
DestinationType destRef = srcRef;
SourceType is a class, DestinationType is a superclass of the subclass SourceType
SourceType is a class, DestinationType is an interface, which is implemented by the class SourceType
SourceType is an interface, DestinationType is Object
SourceType is an interface, DestinationType is superinterface of subinterface SourceType
SourceType is an array, DestinationType is Object
SourceType is an array, DestinationType is an array, where the element of SourceType can be converted to the element of DestinationType


I not only use all the brains that I have, but all that I can borrow. [Laurence J. Peter]
 
 
subject: type conversion