Downcasting with instanceof Operator in Java Concept, Syntax, Examples and Safe Type Casting Prepared by Rachit Joshi 1. Introduction In Java, downcasting means converting a reference of a parent class type into a reference of a child class type. It is useful when a parent - class reference refers to an object actually created from a subclass and the program needs subclass - specific members. The instanceof operator can check the object's runtime type before casting. 2. What is the instanceof Operator? The instanceof operator checks whether an object is an instance of a class, subclass, or interface. It returns true when the object is compatible with the tested type and false otherwise. If the reference is null, instanceof returns false. objectReference instanceof TypeName 3. What is Downcasting? Downcasting changes a reference from a superclass type to a subclass type. It is valid only when the actual object is an instance of that subclass. A parent reference cannot be assigned directly to a child reference without an explicit cast. Animal animal = new Dog(); Dog dog = (Dog) animal; This cast is valid because the object created is a Dog, even though it is stored in an Animal reference. 4. Why Use instanceof Before Downcasting? • It checks the runtime type before a cast is attempted. • It helps avoid ClassCastException when the object is not an instance of the target subclass. • It makes type - dependent logic clearer when working with inheritance or interfaces. • It does not convert an object; it checks type compatibility. 5. Safe Downcasting Example class Animal { } class Dog extends Animal { void bark() { System.out.println("Dog is barking"); } } public class Main { public static void main(String[] args) { Animal animal = new Dog(); if (animal instanceof Dog) { Dog dog = (Dog) animal; dog.bark(); } } } Output: Dog is barking 6. Downcasting Without instanceof Downcasting can work without instanceof when the actual object matches the target type. If it does not, the cast throws ClassCastException at runtime. Animal animal = new Animal(); Dog dog = (Dog) animal; // ClassCastException at runtime 7. Using instanceof with Interfaces A reference typed as an interface can refer to objects of different implementing classes. instanceof can check the implementation before calling implementation - specific methods. interface Printable { } class Report implements Printable { void printReport() { System.out.println("Printing report"); } } Printable item = new Report(); if (item instanceof Report) { Report report = (Report) item; report.printReport(); } 8. instanceof with null If the tested reference is null, instanceof evaluates to false. Dog dog = null; System.out.println(dog instanceof Dog); // false 9. Modern Java Pattern Matching In modern Java versions that support pattern matching for instanceof, the type check and variable declaration can be combined, avoiding a separate explicit cast. if (animal instanceof Dog dog) { dog.bark(); } 10. Important Points to Remember • Downcasting converts a superclass reference to a subclass reference. • The actual runtime object must be compatible with the target subclass. • instanceof checks type compatibility but does not perform the cast itself. • An invalid explicit cast can cause ClassCastException. • instanceof returns false for null references. • Pattern matching for instanceof is available in modern Java versions. 11. Conclusion The instanceof operator is useful for checking an object's runtime type before downcasting in Java. This can help avoid ClassCastException and makes code using superclass or interface references safer. Modern pattern matching can make the check and cast more concise.