
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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.
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.
Syntax
subclassName subclassObjName=(subclassName)superclassObjName;
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.
Rules for Java Object Downcasting
Follow the rules given below while downcasting object otherwise the compiler will throw ClassCastException.
- The object must be an instance of the class you're casting to or its subclass.s.
- The source type and the target cast type must be related through inheritance (either superclass-subclass or subclass-superclass), or be the same type.
- Check the type of object before downcasting.
- Create an object of a class compatible with the target cast type or, cast to a common superclass shared by both types if they are unrelated by direct inheritance.
- Also, the syntax given above should be correct.
Example 1: Wrong Syntax
The following example illustrates what could be the result of providing the wrong syntax of downcasting.
// it is the super class class Info1 { void mesg1() { System.out.println("Tutorials Point"); } } // inheriting super class class Info2 extends Info1 { void mesg2() { System.out.println("Simply Easy Learning"); } } public class Down { public static void main(String[] args) { // Wrong syntax of downcasting Info2 info2 = (Info2) new Info1(); // 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: Casting to an Unrelated Type
When you downcast to an unrelated type, the compiler will produce incompatible types error as shown in the below Java program:
// it is the super class class Info1 { void mesg1() { System.out.println("Tutorials Point"); } } class Info2 { void mesg2() { System.out.println("Simply Easy Learning"); } } public class Down { public static void main(String[] args) { Info1 info1 = new Info2(); // performing downcasting Info2 info2 = (Info2) info1; // calling both methods using object of sub class info2.mesg1(); info2.mesg2(); } }
Output
Down.java:14: error: incompatible types: Info2 cannot be converted to Info1 Info1 info1 = new Info2();
Example 3: Correct Downcasting
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. Also, we use the correct syntax of downcasting.
// it is the super class class Info1 { void mesg1() { System.out.println("Tutorials Point"); } } // inheriting super class class Info2 extends Info1 { 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