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 Cast operator 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 "Cast operator" Watch "Cast operator" New topic
Author

Cast operator

Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
Hi all,
I have code :
class A
{
A()
{}
}


public class B extends A
{
B()
{}

public static void main(String[] args)
{
A a = new A();
B b = new B();
b = (B) a;
}
}
This will be compiled successfully.
But a ClassCastException is thrown when I run this.
Can you help me?
thanks
daniel
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
If the cast were allowed an object of type A could be treated as it were an object of type B, and thus it could receive the messages that were declared in class B but not in class A.
If tricycle is a suclass of bicycle, a tricycle is a kind of bicycle because extends of it. But a bicycle is not a kind of tricycle.


SCJP2. Please Indent your code using UBB Code
Prakash Yamuna
Greenhorn

Joined: Sep 30, 2001
Posts: 5
Upcast and downcast need to be allowed. For example consider a Vector..
Vector v = new Vector();
Button b = new Button();
String s = new Strign();
v.add(b);
v.add(s)
v.get(1)- returns me a an Object whose runtime type is String. I can always treat a java.lang.String object as a java.lang.Object object.
Object o = v.get(1);
String s = (String) o;
This goes through fine since at runtime the type of o is String.
Similar is the case in your example however:
o = v.get(o); //at run time returns a Button object
String str = (String) o; throws an exception since at runtime it is of type Button.
As to why it is allowed at compile time, it is got to do with support for dynamic binding, if dynamic binding is not supported then we couldn't have Vector which can hold any type of object. So in order to support dynamic binding the kind of example provided above is allowed, but since it may result in improper usage the ClassCastException is defined to flag such errors.
Hope this helps.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Adding to what the others have said....
If you replace your original expression A a = new A(); with A a = new B(); ... your code will do just fine
Shyam
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Casting happens at compile time
compiler will approve based on the inheritence policy

But Exception occurs when JVM detects reference 'a' is not pointing to the reference of B .
Ragu
Hassan Naqvi
Ranch Hand

Joined: May 03, 2001
Posts: 158

b = (B) a;
Is ignored at compile time by compiler because you(the programmer) is forcing compiler to do so.This is called Explicit Cast
But at run time an exception is thrown this is because as Jose said,

If tricycle is a suclass of bicycle, a tricycle is a kind of bicycle because extends of it. But a bicycle is not a kind of tricycle.

Regards,
Hassan

Always Belive On Logic!!
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
For completeness about casting:
JLS 5.5
"Some casts can be proven incorrect at compile time; such casts result in a compile-time error.
A value of a primitive type can be cast to another primitive type by identity conversion, if the types are the same, or by a widening primitive conversion or a narrowing primitive conversion.
A value of a primitive type cannot be cast to a reference type by casting conversion, nor can a value of a reference type be cast to a primitive type.
The remaining cases involve conversion between reference types. 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:"
I dont' reproduce them you can read them in JLS. But it follows:
"If a cast to a reference type is not a compile-time error, there are two cases:
* The cast can be determined to be correct at compile time. A cast from the compile-time type S to compile-time type T is correct at compile time if and only if S can be converted to T by assignment conversion (�5.2).
* The cast requires a run-time validity check.
"
I don't reproduce the rules for the runtime validity check.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cast operator
 
Similar Threads
Serialization and deserialization
not wanted methods getting invoked when created an object
inner class ?!
Access a class no package
Constructors...