posted 18 years ago
Hi, vinaykumar, comrade.
Your question is a typical case of casting.
Let's explan it first with primitive values. First you must understand that the hierachy of primitive values is like this...
byte->short->int->long->float->double
...meaning with this that you can always assign a byte variable to a short one, a short one to an int one, and so forth.
The contrary is not always true. Why? Well, for example, a byte varible could contain values from -128 up to 127, and an int variable can easily hold that, but an integer variable could contain -2^31 up to (2^31)-1, so a byte variable cannot always contain an int.
As the compiler cannot know what real values of the variables are going to be at runtime, every time you want to store a wider data type into a narrower one, you will have to explicitly say so to the compiler by means of casting...
That way the compiler makes sure that you know what you're doing, because this conversion might cause loosing some data.
It is the same with Objects. Suppose that...
Object->Mammal->Lion
Object->Mammal->Fox
...is a class hierachy.
This means you can assing to a Mammal object reference to a Lion object reference, like this:
Now, you can do this without casting, because all Lions are Mammals, right? But the contrary is not necesarily true. I mean, not all the Mammals are Lions, correct? It could be a Fox, or any other kind of Mammal.
So, when you want to do downcasting, that means assing an object reference from the parent class to an object reference of the child class you have to explicitly say so by means of the casting operator, like this:
If asLan were not really a Lion, but a Fox, a ClassCastException will be thrown.
I hope all this helps to understand casting. Now give a look at the method signature you're trying to understand and you'll see what I mean.
Regards,
Edwin Dalorzo
[ January 14, 2006: Message edited by: Edwin Dalorzo ]
[ January 14, 2006: Message edited by: Edwin Dalorzo ]