The process of typecasting the object of base class type into derived class type is called as downcasting in Java. The process of Java downcasting is simple. All you have to do is to derive a class from base class by direct or indirect process. An object from base class type is automatically typecasted into derived class. The process is called as downcast because inheritance diagrams are normally written with base class on top and derived class is placed in the bottom level. In downcasting process, a developer will go down the inheritance diagram by deriving objects from base class. The objects derived from base class are converted into objects belonging to derived class.
- SEE: Types of Binding
Let us consider the following program for example:
class Paren1
{
/* …
content goes here
…..*/
}
class ChilClass extends Paren1
{
/* …
…….
;;;;;;;;;
*/
}
public class Testr
{
public static void main (String args[ ])
{
Paren1 p1= new Paren1( );
/*this is a downcast since the Parent class
object, “p1” is being cast to a ChilClass type,
and ChilClass derives from the Paren1 class */
ChilClass c1= (ChilClass) p1;
}
}
The program will compile but throw an exception belonging to the class java.lang.classcastexception. The above will not work during runtime. So why the given code is compiled to throw an exception? The answer is compiler design is different from humans and compiler always assumes that the programmer is always correct. In the program given above the line at which the downcasting performed is ChilClass c1= (ChilClass) p1;
The cast present in the line ChildClass c = (ChildClass) p; will be identified by the compiler as the code which was intentionally done by the programmer. The object “p” present in the casting line will be identified as a type of parent class which will result in a runtime exception.
Let us consider that there is no type cast in the above code. The particular line with typecast will resemble like
ChilClassc = p1;
Is Downcasting is Necessary?
If there is no down cast, the codes in the above program will return with compile time error. Downcasting in Java is a very useful tool when there is a need to compare two objects. The following example will explain how downcasting is used to compare two objects.
public class Person
{
private String name;
private int age;
public boolean equals(Object anObject)
{
if (anObject == null)
return false;
/* The object being passed in is checked
to see it’s class type which is then compared
to the class type of the current class. If they
are not equal then it returns false
*/
else if (getClass( ) != anObject.getClass())
return false;
else
{
/*
this is a downcast since the Object class
is always at the very top of the inheritance tree
and all classes derive either directly or indirectly
from Object:
*/
Person aPerson = (Person) anObject;
return ( name.equals(aPerson.name)
&& (age == aPerson.age));
}
}
}
In the above program downcasting is performed in the equals() method. The equal method is used to compare to objects, to check whether the name and age of these objects are same. If age and name of these objects are similar, the output will be returned as true.
Let us consider that the person class present in the code is used to derive some other classes. If we want to compare those two classes, we may require a equal() method as given above. The equal method will accept any type of object because it is required to compare the objects.
Process to check the legitimization of downcasting
Let us consider the following code for example
else if (getClass( ) != anObject.getClass())
return false;
If the getclass() method in the above program is allowed to run, it will enable us to acquire the class of the object. The acquired object will be passed in and will be compared to the class of current class. If the both the classes are not equal, it is unnecessary to use downcasting.