• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

typecasting of reference variable doubt

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Ranch Hand
Posts: 91
Firefox Browser C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 82
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)..
 
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
once again, welcome to JavaRanch samarjeet singh
 
samarjeet singh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks to all for your response.
 
samarjeet singh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic