Rules of Downcasting Objects in Java


In Java, downcasting is the process of converting an object of parent class to object of child class. We need to perform the conversion explicitly. It is quite similar to what we do in primitive typecasting.

In this article, we will learn about downcasting and what are the rules that we must follow to downcast an object in Java.

Object Downcasting in Java

Earlier we discussed that downcasting is somewhat similar to typecasting. However, there exist a few differences too. The first is that we can type cast only primitive datatypes and the second is that it is irreversible action i.e. in typecasting we work directly with values and if we change the value once, we cannot restore it. On the other hand in the downcasting process, the original object does not get changed, only its type changed.

Need for Downcasting

For an instance, assume that there are two classes, one is the super class and the other one is its sub class. Here, the sub class contains a few features that its super class wants to use. In this case, downcasting comes into the picture, it allows us to access the member variables and methods of sub class to super class.

Syntax

nameOfSubclass nameOfSubclassObject = (nameOfSubclass) nameOfSuperclassObject; 

Although this syntax seems so easy, many of us can do mistakes while writing this syntax. Also, we can’t directly downcast objects like we are going to do in the next example. If we do so the compiler will throw ‘ClassCastException’.

Example 1

The following example illustrates what could be the result of providing the wrong syntax of downcasting. For this, we will define two classes and try to downcast the object of super class with wrong syntax.

class Info1 {  
// it is the super class
   void mesg1() { 
      System.out.println("Tutorials Point"); 
   }
} 
class Info2 extends Info1 {
// inheriting super class
   void mesg2() { 
      System.out.println("Simply Easy Learning"); 
   }
} 
public class Down {
   public static void main(String[] args) { 
      Info2 info2 = (Info2) new Info1(); 
      // Wrong syntax of downcasting
      // calling both methods using sub class object
      info2.mesg1(); 
      info2.mesg2(); 
   } 
}

Output

Exception in thread "main" java.lang.ClassCastException: class Info1 cannot be cast to class Info2 (Info1 and Info2 are in unnamed module of loader 'app')
   at Down.main(Down.java:13)

Example 2

The following example demonstrates how we can perform object downcasting properly. For this, we will create two classes and then, we define an object of the super class that refers to sub class. In the end, we use the correct syntax of downcasting.

class Info1 {
// it is the super class
   void mesg1() { 
      System.out.println("Tutorials Point"); 
   }
} 
class Info2 extends Info1 {  
// inheriting super class
   void mesg2() { 
      System.out.println("Simply Easy Learning"); 
   }
} 
public class Down { 
   public static void main(String[] args) { 
      // object of super class refers to sub class
      Info1 info1 = new Info2(); 
      // performing downcasting
      Info2 info2 = (Info2) info1;
      // calling both methods using object of sub class 
      info2.mesg1(); 
      info2.mesg2(); 
   } 
}

Output

Tutorials Point
Simply Easy Learning

Conclusion

There is one more concept for modifying objects named Upcasting. In this process, the object of sub class gets converted to super class object. It can be done implicitly. Both the concepts, upcasting and downcasting together called object casting. In this article, we have discussed object downcasting with the help of examples.

Updated on: 15-May-2023

413 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements