This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

type conversion

 
sun moon
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This rule is used when you have code like:
 
sun moon
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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 ]
 
Karin Paola Illuminate
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic