This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Class casting/assigning question - REVISED Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class casting/assigning question - REVISED" Watch "Class casting/assigning question - REVISED" New topic
Author

Class casting/assigning question - REVISED

David Miranda
Ranch Hand

Joined: Mar 14, 2005
Posts: 34
Hi all,

Im trying to understand what the Java checks for regarding illegal casts during compile time and runtime.

In this program, I've created four classes, AClass, BClass (which extends AClass), CClass (which extends BClass), and the Tester class. The Tester class creates an object,a, with reference to AClass, but instantiated with CClass. It then creates another object,b, with reference to BClass that holds the value (references the value?) of a copied object a casted into BClass...Finally, the code prints out BClass' i value.

As I understand it...in this statement

AClass a = new CClass(); <--- AClass is reference type and CClass is compile-time type. (Please correct me if wrong)

My Questions Are:
1) What does the Java compiler check for in regards to object assignment and casting? Does it just check to see if compile-time type assignments are valid?


2) What does the Java runtime environment check for in regards to object assignment and casting?


public class Tester {
public static void main(String args[]) {
AClass a = new CClass();
BClass b = (BClass) a;
System.out.println(b.i);
}
}

class AClass {
int i = 5;
void method() {
System.out.println("Class A Method");
}
}

class BClass extends AClass {
int i = 6;
void method() {
System.out.println("Class B Method");
}
}

class CClass extends BClass {
int i = 7;
void method() {
System.out.println("Class C Method");
}
}

I apologize if this question is vague...

Thanks,

Dave
[ April 15, 2005: Message edited by: David Miranda ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

AClass a = new CClass();

AClass is the declared type of the reference, so AClass is the compile-time type. However, the actual object being created is of type CClass, so CClass is the runtime type. The reference to the CClass object is upcast to AClass through "assignment conversion."

What does the Java compiler check for in regards to object assignment and casting? Does it just check to see if compile-time type assignments are valid?

Basically, the compile-time types must have an inheritance relationship. If it's an upcast (or conversion), then no explicit cast is needed because this is always safe. For example, a VWJetta is always a Car. However, if it's a downcast, then an explicit cast is required because this is potentially unsafe. For example, a Car is not always a VWJetta. (If the explicit cast is incorrect, see below...)

What does the Java runtime environment check for in regards to object assignment and casting?

At runtime, the actual runtime type is checked. If the cast is invalid, then a ClassCastException is thrown.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
David Miranda
Ranch Hand

Joined: Mar 14, 2005
Posts: 34
Thanks for clearing that up!
 
 
subject: Class casting/assigning question - REVISED
 
Similar Threads
Default constructor
Can anyone explain executing at runtime vs compile?
Class name's as Identifiers
Reference variable code
Modifier protected does not hide inherited members to (static) code of superclass package