This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting reference question

 
Vinny Srin
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic