Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

typecasting of reference variable doubt

 
samarjeet singh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,i am not able to understand the typecasting(upcasting & downcasting ) of reference variable could anybody please help me out!!!
 
Pritish Chakraborty
Ranch Hand
Posts: 91
C++ Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
samarjeet singh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
once again, welcome to JavaRanch samarjeet singh
 
samarjeet singh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all for your response.
 
samarjeet singh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic