Saturday, June 1, 2013

Upcasting and Downcasting in java

You can cast an object to another class type, but only if the current object type and the new class type are in the same hierarchy of derived classes, and one is a super-class of the other. Upcasting and downcasting are important part of java. The concept of Polymorphism can help you to better understand the meaning of upcasting and downcasting.

You can cast a reference to an object of a given class type upwards through its direct and indirect superclasses. For example, you could cast a reference to an object of type Spaniel directly to type Dog, type Animal, or type Object. You could write:

Spaniel aPet = new Spaniel(“Fang”);
Animal theAnimal = (Animal)aPet;
Dog theDog = (Dog)aPet;
Object theObject = (Object)aPet;

When you are assigning an object reference to a variable of a superclass type, you do not have to include the cast. You could write the assignment as:
Animal theAnimal = aPet;  // Cast the Spaniel to Animal
This would work just as well. The compiler is always prepared to insert a cast to a superclass type when necessary.

In simple term, we define polymorphism as " Having multiple forms" or " Having many forms". Consider the following. Ask yourself what is rectangle? Most would say its shape. A square,  circle and a triangle are also shapes. What I'm saying is that shape can take many forms or has multiple forms.

Polymorphism in Java
Class inheritance is not just about reusing classes that you have already defined as a basis for defining a new class. It also adds enormous flexibility to the way in which you can program your applications, with a mechanism called polymorphism. So what is polymorphism?

The word polymorphism generally means the ability to assume several different forms or shapes. In programming terms it means the ability of a single variable of a given type to be used to reference objects of different types and to automatically call the method that is specific to the type of object the variable references. This enables a single method call to behave differently, depending on the type of the object to which the call applies

First we create a base class shape. This base class implement a constructor that will accept 2 arguments and a method that will draw out shape.

We need to make out method draw() to behave polymorphically.

What we have done is created an array of the type Shape. Because Square and Circle are derived from Shape, we are able to put them in our array. What we are then doing is looping through all the elements of our array and calling draw for each of our types. Because we have overridden the draw method in each of our derived classes the output of our code is:
Draw a shape at 100,100
Draw a square at 200,200
Draw a circle at 300,300
If we did not override Draw in one of our derived classes, the base class implementation of Draw would be called

Upcasting and Downcasting
First, you must understand, that by casting you are not actually changing the object itself, you are just labeling it differentlyFor example, if you create a circle and upcast it to shape, then the object doesn't stop from being a circle. It's still a circle, but it's just treated as any other shape and it's circle properties are hidden until it's downcasted to a circle again.


Circle c=new Circle();
System.out.println(c);
Shape s=c;
System.out.println(s);

Output will be look like this :
Circle@12b6651
Circle@12b6651

As you see circle is still exactly the same after upcasting, it didn't change to shape, its just being labeled as shape. This is allowed because circle is a shape.
There is no need to do upcasting manually, its allowed to do.
Shape s = (Shape)new Circle();
same as
Shape s = new Circle();

But downcasting must always be done manually:
Circle c = new Circle();
Shape s=c;
Circle c1 = (Circle)s;// manual downcasting to circle

Why upcasting is automatic and downcasting is manual because upcasting never fail. If you group of different shapes and want to downcast all of them to circle, then there may be chance, that some of shape are rectangle and code fail by throwing ClassCastException.

Consider this example;
shape s=new shape();
shape s=new circle();

You can store circle as a shape, and you can downcast it to circle when appropriate but doing this might be a sign of bad design.  On the other hand, compiler will let you to cast "s" to a circle, but you'll get a runtime error because the instance isn't actually a circle.

When to Cast Objects
You will have cause to cast objects in both directions through a class hierarchy.

You will cast object in upwards:

  • whenever you execute methods polymorphically, you are storing objects in a variable of a base class type and calling methods in a derived class. This generally involves casting the derived class objects to the base class
  • You want to cast up through a hierarchy is to pass an object of several possible subclasses to a method. By specifying a parameter as a base class type, you have the flexibility to pass an object of any derived class to it. You could pass a Dog, Duck, or Cat object to a method as an argument for a parameter of type Animal.
You will cast object in downwards:

The reason you might want to cast down through a class hierarchy is to execute a method unique to a particular class. If the Duck class has a method layEgg(), for example, you can't call this using a variable of type Animal, even though it references a Duck object. As I said, casting downward through a class hierarchy always requires an explicit cast.
The object pointed to by aPet is first cast to type Duck. The result of the cast is then used to call the method layEgg(). If the object were not of type Duck, the cast would cause an exception to be thrown.


If you know anyone who has started learning java, why not help them out! Just share this post with them. Thanks for studying today!...

2 comments:

  1. Casting is also used for Boxing and Unboxing conversions.
    especially it may be useful for achieving Boxing conversions.
    eg. for an implementation of a Comparator
    public int compare(Account ac1, Account ac2) {
    return ((Long)(ac1.getBalance())).compareTo(ac2.getBalance());
    }

    ReplyDelete