Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Class casting/assigning question - REVISED  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Miranda
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing that up!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!