This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting reference question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting reference question" Watch "Casting reference question" New topic

Casting reference question

Vinny Srin

Joined: Feb 25, 2002
Posts: 6
Class Myclass {
public static void main (String args[]) {
MyClass a = new MyClass();
MySubclass b = new MySubclass();
a = b; //1
b = a; // 2
Class MySubclass extends Myclass{}

Compiler errors at 2. Answer key says the error is because we are attempting to assign a superclass reference "a" to a subclass "b". How?
At (1) arent we changing the superclass reference "a" to subclass reference "b"?
Therefore Should'nt the answer say that we are failing to assign subclass reference to a superclass in 2?
Please clarify
Rick Salsa
Ranch Hand

Joined: Jul 17, 2001
Posts: 173
Hi Vinny,
At compile time, the complier sees it as a trying to save a reference of the super class to a subclass. To get this to work, we would need to provide a cast for it.

When you compile it, your complier should say something about incompatible types. So they answer they gave you is right. Only at compile time does the VM know that a actually refers to an object of MySubclass.
[ February 28, 2002: Message edited by: rick salsa ]
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Here is a simple way to look at things, take a look at the following classes:
class Animal {}
class Dog extends Animal {}
class Cat extends Animal {}
we can say that:
1) a dog 'is-a' animal
2) a cat 'is-a' animal
in code we say it like this
Dog dog = new Dog();
Cat cat = new Cat();
Animal ani;
ani = cat; // cat is an animal
ani = dog; // dog is an animal
since both 'dog' and 'cat' are animals, we can assign them to the animal reference!
now we can't say that
an animal is a 'cat'
an animal is a 'dog'
why? because it's impossible for an animal to be a dog and also a cat! have you ever seem a catdog
so that is why you can't write code like:
Animal ani = new Animal();
Dog dog = ani; // can't do this
Cat cat = ani; // can't do this either!
Hope that help

<a href="" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Rick is correct that you can use typecasting to make the code compile, but when you try to run it you will get a "java.lang.ClassCastException" runtime exception!
So I would recommend not writing code to 'down-cast' classes.
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
So I would recommend not writing code to 'down-cast' classes.
...Unless you know it is safe to do so...that is, you know what kind of class to expect when you downcast.
Sometimes you cannot avoid downcasts because your design uses some kind of generic way to handle things.
my $.2
[ February 28, 2002: Message edited by: Valentin Crettaz ]

[Blog] [Blogroll] [My Reviews] My Linked In
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Valentin you're correct, thanks to catching that one!
Come to think of it you might pass around references of type 'Component' and then 'down-cast' to the correct component class like a Button, etc. if you know what that component is
Vinny Srin

Joined: Feb 25, 2002
Posts: 6
Still having trouble.
From the code I used, what exactly happens when I say
a = b ?
I know for one, the compiler is checking if class reference b is a subclass of class reference "a". Then is it not actually changing the current reference of a to b?
So before going to the next line of code, is it not true that 'a' is actually pointing to a subclass 'b' reference at this point?
Moving to the next line
b = a
here 'a' (holding a reference to subclass) is being assigned another reference of subclass itself. Which is againt the rule that states you can only assign class type to a superclass type without explicit cast. Please clarify.
thanks for being patient. Only trying to understand.
Rick Salsa
Ranch Hand

Joined: Jul 17, 2001
Posts: 173
The problem here Vinny is the complier. We can all see that it is type-safe, but the complier doesn't really know what the actual reference of a or b is at compile time. It just knows that a is of type Myclass and B is of type MySubclass. So at compile time, it is not legal to do b = a. The compiler needs an explicit cast telling it that you accept the risk that the types might not be compatible.
At runtime, the VM checks to see if a is acutally the same type or a subclass of b. If it is, the cast can be done. If not, you will get a ClassCastException error.
Remember you can always cast up the hierarchy tree, but a downcast requires a explicit cast.
Hope that helps,
I agree. Here's the link:
subject: Casting reference question
Similar Threads
3 Questions
null and Classes ??
private final method
question from mughals book
Garbage collection