GeeCON Prague 2014*
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


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting reference question" Watch "Casting reference question" New topic
Author

Casting reference question

Vinny Srin
Greenhorn

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="http://www.rajindery.com" 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
Sheriff

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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[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
Greenhorn

Joined: Feb 25, 2002
Posts: 6
Folks
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,
/rick
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting reference question