Object Type Casting in Java : Free Java Tutorials

Object Type Casting in Java says that one object reference can be typecast into another object reference. We can use parent reference to hold child object. For example, Object class reference can hold String class object.
Object str = new String();

We can also use interface reference to hold implemented class object.
Runnable thread = new Thread();

Suppose we perform object typecasting in the following way.
A b = (C) d;

Where ‘A’ is class/interface name, ‘b’ is the name of the reference variable, ‘C’ is class/interface name and, ‘d’ is reference variable name.


We must take care of following rules while defining such object typecasting.

Rule 1: Compile-time checking 1

The type of ‘d’ and ‘C’ must have some relation either child to parent or parent to child or same type. Otherwise, we will get following compile-time error:

Inconvertible types

found: d type

required: C

Object Type Casting: Example 1

// Main class
public class Demo
{
	public static void main(String[] args)
	{
		// Creating string object that has reference of Object class
		Object obj = new String("Meet");
		
		// type casting String object into StringBuffer object
		StringBuffer strbfr = (StringBuffer)obj;
	}
}

There will not be any compile-time error in the above code as it follows rule 1.

Rule 2: Compile-time checking 2

‘C’ must be either same or derived type of ‘A’. Otherwise, we will get a compile-time error saying:

Inconvertible types

found: C

required: A

Second Example of Object Type Casting

// Main class
public class Demo
{
	public static void main(String[] args)
	{
		// Creating string object that has reference of Object class
		Object obj = new String("Meet");
		
		// type casting obj into String object and then assigning it StringBuffer reference
		StringBuffer strbfr = (String)obj;
	}
}

The above code follows rule 1. But, it does not follow rule 2 because String class is not child class of StringBuffer class.

Hence, we will get a compile-time error saying Type mismatch: cannot convert from String to StringBuffer.

Until now, we understood two rules with respect to the compiler’s job. Our 3rd and the most important rule is related to JVM’s responsibility.

Rule 3: Run-time checking

Run-time object type of ‘d’ must be either same or derived type of ‘C’. Otherwise, we will get a run-time exception saying ClassCastException.

Example 1 does not follow rule 3. And we will get following run-time exception while running that program.

Exception in thread "main" java.lang.ClassCastException: java.base/java.lang.String cannot be cast to java.base/java.lang.StringBuffer
	at Demo.main(Demo.java:10)

Let’s take one final example where all three rules are satisfied.

// Main class
public class Demo
{
	public static void main(String[] args)
	{
		// Creating string object that has reference of Object class
		Object obj1 = new String("Meet");
		
		// type casting obj1 into String object and then assigning it Object class reference
		Object obj2 = (String)obj1;
	}
}

Here, String class is a subclass of Object class and String object is typecast into String object only.

So, above code follows all the rules and it will not give any run-time or compile-time error.This is all about Object Type Casting in Java. Hope you find the tutorial helpful.

Leave a Comment

Your email address will not be published. Required fields are marked *