Class Type Casting in Java Last Updated : 17 Sep, 2021 Comments Improve Suggest changes Like Article Like Report Typecasting is the assessment of the value of one primitive data type to another type. In java, there are two types of casting namely upcasting and downcasting as follows: Upcasting is casting a subtype to a super type in an upward direction to the inheritance tree. It is an automatic procedure for which there are no efforts poured in to do so where a sub-class object is referred by a superclass reference variable. One can relate it with dynamic polymorphism.Implicit casting means class typecasting done by the compiler without cast syntax.Explicit casting means class typecasting done by the programmer with cast syntax.Downcasting refers to the procedure when subclass type refers to the object of the parent class is known as downcasting. If it is performed directly compiler gives an error as ClassCastException is thrown at runtime. It is only achievable with the use of instanceof operator The object which is already upcast, that object only can be performed downcast. In order to perform class type casting we have to follow these two rules as follows: Classes must be “IS-A-Relationship “An object must have the property of a class in which it is going to cast. Implementation: (A) Upcasting Example 1 Java // Importing input output classes import java.io.*; // Class 1 // Parent class class Parent { // Function void show() { // Print message for this class System.out.println("Parent show method is called"); } } // Class 2 // Child class class Child extends Parent { // Overriding existing method of Parent class @Override // Same Function which will override // existing Parent class function void show() { // Print message for this class System.out.println("Child show method is called"); } } // Class3 // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating a Parent class object // but referencing it to a Child class Parent obj = new Child(); // Calling the show() method to execute obj.show(); } } OutputChild show method is called Output explanation: Here parent class object is called but referred to the child's class object. Hence, one can relate this with dynamic polymorphism or function overriding. (B) Downcasting Example 2 Java // Java Program to illustrate Downcasting // Importing input output classes import java.io.*; // Class 1 // Parent class class Vehicles { } // Class 2 // Child class class Car extends Vehicles { static void method(Vehicles v) { // if (v instanceof Car) { // Downcasting Car c = (Car)v; // Display message System.out.println("Downcasting performed"); } } // Main driver method public static void main(String[] args) { // Creating an object of Vehicle class // and referring it to Car class Vehicles v = new Car(); Car.method(v); } } OutputDowncasting performed NOTE : Without perform upcast if we try to downcast , ClassCastException will be thrown. It is a runtime exception or unchecked exception.It is class, present in java.lang package.It can be avoided by using a operator known as 'instanceof'. Example 3 Java // Java Program showing ClassCastException // Importing input output classes import java.io.*; // Class 1 // Parent class/ Member class class Member { // Member variable of this class String name; long phone; // Member function of this class void chat() { // Print message of Member/ Child class System.out.println( name + " : chatting in whatsapp group"); } } // Class 2 // Child class/ Admin class class Admin extends Member { // Member function of this class void addUser() { // Print message of Admin/ Parent class System.out.println( name + " : adding a new user in whatsapp group"); } } // Class3 - Main class class GFG { // Main driver method public static void main(String[] args) { // Creating an object Ad Member mem = new Admin(); // Upcasting access only general property of // superclass // Custom entry for Member class mem.name = "Sneha"; mem.phone = 9876543210l; // Calling function mem.chat(); Admin ad = (Admin)mem; // Downcast to access specific property of subclass ad.addUser(); } } OutputSneha : chatting in whatsapp group Sneha : adding a new user in whatsapp group Comment More infoAdvertise with us Next Article Class Type Casting in Java D devefullstack404 Follow Improve Article Tags : Java Java Programs Java-Object Oriented java-inheritance Practice Tags : Java Similar Reads Java Program to Implement Type Casting and Type Conversion There are many situations that come when we have to change the functionality of the output as well as the type of output according to the need of the requirements. For e.g. Entered PI value in decimal format should be converted in the decimal format so that value can be used efficiently without any 3 min read Creating a Generic Array in Java Arrays in Java are generated using a certain data type. On the other hand, you may create a generic array that functions with various object types by utilizing generics. You can build type-safe, reusable code by using generics. In this article, we will learn about creating generic arrays in Java.Jav 2 min read Which Data Type Cannot be Stored in Java ArrayList? The ArrayList class implements a growable array of objects. ArrayList cannot hold primitive data types such as int, double, char, and long. With the introduction to wrapped class in java that was created to hold primitive data values. Objects of these types hold one value of their corresponding prim 4 min read Java Program For Int to Char Conversion In this article, we will check how to convert an Int to a Char in Java. In Java, char takes 2 bytes (16-bit UTF encoding ), while int takes 4 bytes (32-bit). So, if we want the integer to get converted to a character then we need to typecast because data residing in 4 bytes cannot get into a single 3 min read Convert Long Values into Byte Using Explicit Casting in Java In Java, a byte can contain only values from -128 to 127, if we try to cast a long value above or below the limits of the byte then there will be a precision loss. 1. byte: The byte data type is an 8-bit signed twoâs complement integer. Syntax: byte varName; // Default value 0 Values: 1 byte (8 bits 2 min read Converting Integer Data Type to Byte Data Type Using Typecasting in Java In primitive casting, we can convert the byte data type information into the integer data type information by using implicit typecasting. When we try to convert the integer data type information into the byte data type information that time explicit type casting takes place. Explicit Type of Castin 2 min read Java Program to Convert int to long Given a number of int datatype in Java, your task is to write a Java program to convert this given int datatype into a long datatype. Example: Input: intnum = 5 Output: longnum = 5 Input: intnum = 56 Output: longnum = 56 Int is a smaller data type than long. Int is a 32-bit integer while long is a 6 2 min read Java Program to Convert long to int Long is a larger data type than int, we need to explicitly perform typecasting for the conversion. Typecasting is performed through the typecast operator. There are basically three methods to convert long to int: By type-castingUsing toIntExact() methodUsing intValue() method of the Long wrapper cla 3 min read Java Program to Convert Byte Array to Object Converting byte array into Object and Object into a byte array process is known as deserializing and serializing. The class object which gets serialized/deserialized must implement the interface Serializable. Serializable is a marker interface that comes under package 'java.io.Serializable'.Byte Arr 2 min read Java Program to Convert String to Object In-built Object class is the parent class of all the classes i.e each class is internally a child class of the Object class. So we can directly assign a string to an object. Basically, there are two methods to convert String to Object. Below is the conversion of string to object using both of the me 2 min read Like