This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cast operator

 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prakash Yamuna
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic