Last Updated : 12 Jul, 2025
Typecasting is one of the most important concepts which basically deals with the conversion of one data type to another datatype implicitly or explicitly. In this article, the concept of typecasting for objects is discussed.
Just like the data types, the objects can also be typecasted. However, in objects, there are only two types of objects, i.e. parent object and child object. Therefore, typecasting of objects basically means that one type of object (i.e.) child or parent to another. There are two types of typecasting. They are:
The following image illustrates the concept of upcasting and downcasting:
Example: Let there be a parent class. There can be many children of a parent. Let's take one of the children into consideration. The child inherits the properties of the parent. Therefore, there is an "is-a" relationship between the child and parent. Therefore, the child can be implicitly upcasted to the parent. However, a parent may or may not inherits the child's properties. However, we can forcefully cast a parent to a child which is known as downcasting. After we define this type of casting explicitly, the compiler checks in the background if this type of casting is possible or not. If it's not possible, the compiler throws a ClassCastException.
Let's understand the following code to understand the difference:
// Java program to demonstrate
// Upcasting Vs Downcasting
// Parent class
class Parent {
String name;
// A method which prints the
// signature of the parent class
void method()
{
System.out.println("Method from Parent");
}
}
// Child class
class Child extends Parent {
int id;
// Overriding the parent method
// to print the signature of the
// child class
@Override void method()
{
System.out.println("Method from Child");
}
}
// Demo class to see the difference
// between upcasting and downcasting
public class GFG {
// Driver code
public static void main(String[] args)
{
// Upcasting
Parent p = new Child();
p.name = "GeeksforGeeks";
//Printing the parentclass name
System.out.println(p.name);
//parent class method is overridden method hence this will be executed
p.method();
// Trying to Downcasting Implicitly
// Child c = new Parent(); - > compile time error
// Downcasting Explicitly
Child c = (Child)p;
c.id = 1;
System.out.println(c.name);
System.out.println(c.id);
c.method();
}
}
GeeksforGeeks Method from Child GeeksforGeeks 1 Method from Child
An illustrative figure of the above program:
From the above example we can observe the following points:
Parent p = new Child();
// This variable is not // accessible p.id = 1;
Child c = (Child)p;
c.name = p.name; i.e., c.name = "GeeksforGeeks"
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4