aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes typecasting of reference variable doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "typecasting of reference variable doubt" Watch "typecasting of reference variable doubt" New topic
Author

typecasting of reference variable doubt

samarjeet singh
Greenhorn

Joined: Jul 30, 2012
Posts: 9
hi,i am not able to understand the typecasting(upcasting & downcasting ) of reference variable could anybody please help me out!!!
Pritish Chakraborty
Ranch Hand

Joined: Jun 12, 2012
Posts: 91

Welcome to the forum!

Please be more specific...post an example and tell us what you are not able to understand.

We will help clarify everything!


OCJP 6
samarjeet singh
Greenhorn

Joined: Jul 30, 2012
Posts: 9
class Animal { }
class Dog extends Animal
{ }
class DogTest{
public static void main(String [] args)
{
Animal animal =new animal();
Dog d=(Dog) animal;//compiles but fails
}
}
/*could you please explain how actually upcasting and downcasting takes place.
Don Redd
Ranch Hand

Joined: Jan 05, 2012
Posts: 82

samarjeet singh wrote:class Animal { }
class Dog extends Animal
{ }
class DogTest{
public static void main(String [] args)
{
Animal animal =new animal();
Dog d=(Dog) animal;//compiles but fails
}
}
/*could you please explain how actually upcasting and downcasting takes place.



Compiler doesn't know what animal variable if referring to at compile time, i.e it may refer to Dog, Animal or any other subclass of Animal.
Since Dog is subclass of Animal ,Compiler allows casting assuming animal is referring to Dog object
but at run time you can not cheat JVM by fitting in Animal(SuperClass) to Dog(Sub Class)..
Sumit Bisht
Ranch Hand

Joined: Jul 02, 2008
Posts: 329

Going by your example,
Dog extends Animal, thus it IS-A specific type of animal. So, a dog knows all about an animal/ a dog displays all characteristics of an animal.
However, an Animal does not know anything about a Dog, so typecasting this into a more specific type can never succeed. Think about it, at runtime an Animal (which could even be human) might be asked to perform a Dog specific work (like barking), which will clearly be illegal.
Only a polymorphic object (that is Actually a Dog object) will work at runtime too (Because it is actually a dog ! .

Animal animal = new Dog();
Dog d = (Dog) animal; // Compiles and runs.

the opposite of this is upcasting where any Dog object might be casted into Animal class.
Please do note that now even if the object is a Dog, it can only perform Animal specific tasks. Trying to do Dog (or any other subclass specific task will simply not compile)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

once again, welcome to JavaRanch samarjeet singh
samarjeet singh
Greenhorn

Joined: Jul 30, 2012
Posts: 9
thanks to all for your response.
samarjeet singh
Greenhorn

Joined: Jul 30, 2012
Posts: 9
i want to crack scjp as i am beginner could you please suggest how should I prepare for it.and thanks to all for my warm welcome
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9243
    
    1

samarjeet singh wrote:class Animal { }
class Dog extends Animal
{ }
class DogTest{
public static void main(String [] args)
{
Animal animal =new animal();
Dog d=(Dog) animal;//compiles but fails
}
}
/*could you please explain how actually upcasting and downcasting takes place.


Remember always that a Sub-class can see a Super-class and not the vice-versa. Now use this logic to figure out how the down-cast and up-cast works.


SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 923
    
    1

these rules will help. but don't memorize them. they are based on concepts based on above posts. consider

A B =(C) D // generalized expression

1. C and D must have a IS-A relation( no matter C IS-A D or viceversa). if this is not the case COMPILER ERROR will occur.

2. A and C must be of the same type or C must be subtype of A , otherwise COMPILER ERROR will occur

3. the runtime type of D must be same as that of C or subtype of C , otherwise RUNTIME ERROR will occur.

consider example

Dog d = new Dog();
String a = (String)d; // violates the first rule

now lets take your case

Dog d=(Dog) animal.

first requiremetn is met. there is a relation between Dog and animal. second requirement is also met. now runtime type of animal is Animal which is neither Dog nor subtype of Dog. so runtime exception occurs. logically you see how can you type cast Animal to Dog. Animal is NOT Dog. but Dog IS-A Animal. it has all the behaviour and attributes of Animal and adds its own.


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 367
    
    1

samarjeet singh wrote:i want to crack scjp as i am beginner could you please suggest how should I prepare for it.and thanks to all for my warm welcome

All you need is K&B... Just read it thoroughly and you are done..

And Best of luck for your preparation
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

While casting, the compiler just checks the inheritance tree. If the cast is in the inheritance hierarchy of the reference variable, the compiler okay's it. This is the reason we use the "instanceof" operator before doing a cast.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: typecasting of reference variable doubt
 
Similar Threads
Casting Doubt
Inheritance question - Interfaces
java.lang.ClassCastException: Parent
A curious case of instanceof keyword usage in JDK 1.5
Anyhting like super.super?