Forums Register Login

Class casting/assigning question - REVISED

+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thanks for clearing that up!
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1024 times.
Similar Threads
Default constructor
Modifier protected does not hide inherited members to (static) code of superclass package
Class name's as Identifiers
Can anyone explain executing at runtime vs compile?
Reference variable code
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:58:45.