Home > Java Basics > Casting Reference Variables- Downcasting, Upcasting

Casting Reference Variables- Downcasting, Upcasting

Let us consider two classes class Animal, a more generic class and class Cat, a more specific class i.e a more specific animal. Let us settle for simple definitions for both the classes so as to get the concept right.

/*
The definition for more generic class Animal
Notice the eat() method, which will be inherited by all Classes extending Animal
*/

class Animal
{

public void eat()
{

System.out.println(“Animal  Eating“);

}

}

/*
Definition for more specific Animal i.e Cat, extends Animal
Notice the overridden eat() method and an extra method goHunting()
*/

class Cat extends Animal
{

public void goHunting()
{

System.out.println(“Do Cat related Hunting”);

}

/*
Overridden eat() method
*/

public void eat()
{

System.out.println(“Cat specific Eating”);

}

}

With a Animal reference pointing to Animal object on the heap one can access only the eat() method of Animal class.

With a Animal reference pointing to Cat object on the heap one can access the overridden version of eat() method in the Cat class (Polymorphism at work here), but even though the object has goHunting() method it cannot be invoked using a reference of type Animal.

With a reference of type Cat pointing to Cat object on the heap, its a pretty straight forward one.

Suppose there is a method getAppointment() in class Veterinarian which has to work for all arguments of type Animal, then the parameter is declared as a more generic Animal.

If I pass a Cat object to the method and try to access the goHunting() method there would be a compiler error “Cannot Find Symbol”. So one has to cast the object passed into Cat and then access the method. The following code snippet explains the thing.

/*
The class Veterinarian definition
The definition and the method definition are fictitious. May not be practical
But they are syntactically correct
*/

public class Veterinarian
{

String name;
public Veterinarian(String name)
{

this.name=name;

}
public static void main( String [] args)
{

Cat c = new Cat();
Veterinarian vet = new Veterinarian(“Dr. XXXXXX“);
vet.getAppointment(c);

}
public void getAppointment(Animal a)
{

Cat c = (Cat) a;
c.goHunting();

}

}

The casting done in the above code snippet is called “Down Casting“. Down Casting is nothing but casting down the inheritance tree to a more specific class i.e casting a superclass to subclass.

Down casting has to be done carefully as it can lead to run time exception java.lang.ClassCastException. There’s some danger lurking in the above code snipet. Can you find it out? Yeah you are right. The casting has been done without considering the fact that the object passed is a Cat or not. If i pass a Dog object and try to cast it into Cat, compiler would say you are doing correct thing. But the code blows up at run time. So before casting it has to be checked whether the passed object it as instance of the class to which we are going to cast. The above getAppointment() method can be modified into a more safer code below:

public void getAppointment(Animal a)
{

if ( a instanceof cat)
{

Cat c = (Cat) a;
c.goHunting();

}

}

There are some places where casting fails at compile time itself.

Animal a = new Animal();
Cat c = (Cat)a;//Blows up at Run time

String s  = (String) a; //Compiler will kill you for doing this. Compiler Error

Upcasting:
Upcasting- casting up the inheritance tree to a more general type. It is done implicitly (i.e typing in the cast is not required). Its because the classes at lower level of hierarchy have all those features that are present in the classes above them in the inheritance hierarchy. So they can be upcasted implicitly ( like Dog IS-A Animal).

Advertisements
  1. November 13, 2008 at 7:42 pm

    Nice Example

  2. November 13, 2008 at 10:09 pm

    @Hemant
    Thanks. It was a pretty natural example.

  3. Nasser Al-Hammad
    April 20, 2009 at 12:56 am

    Thank you alot that was really useful and i have a final tomorrow

  4. Dilip Mane
    May 14, 2009 at 12:19 pm

    Nice Article……..
    Can u please specify where these Up and Down casting is necessary ?????

    • June 6, 2009 at 8:07 pm

      Thanks!

      Once u start coding extensively u will find the use of casting. But its better to be avoided.

  5. Tharaka
    September 6, 2009 at 1:04 am

    Why we need down casting rather than make a new object ?

  6. December 4, 2009 at 10:58 am

    precise and very helpful. thanks a lot.

  7. Stathis
    January 29, 2010 at 3:15 am

    This is not the end of the story!
    Given Animal myAnimal= new Cat(); // cat extends animal ok!
    Does Cat felix = myAnimal; //work? No it does not!
    or Cat felix = (Cat)myanimal; //Yes it does !
    if you run
    public static void main(String[] args) {
    Animal myAnimal = new Cat();

    Boolean b =myAnimal instanceof Cat;// The great power of Autoboxing!
    if(b){
    System.out.println(“myAnimal is a cat = “+ b) ;
    }
    else{
    System.out.println(“I hate dogs”) ;
    }
    }

    you get : myAnimal is a cat = true!
    Why we need a cast ? Find it out yourself!

    • January 30, 2010 at 7:35 pm

      You can read about the Casting information from the JLS Specification- http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5

      One can assign subclass instances to super class references but vice versa is not possible. In your example- though myAnimal is an instance of Cat but at compile time the compiler checks that its an Animal. So assigning superclass reference to subclass reference is “error”.

  8. samjosh
    May 4, 2010 at 4:27 pm

    Wow.. Very candid and nicely explained. thanks a mil

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: