Industry Ready Java Spring Boot, React & Gen AI — Live Course
JavaInheritance

Upcasting and Downcasting

1. Introduction

When working with inheritance, Java allows you to treat an object of a subclass as if it were an object of its parent class.
This is known as type casting in inheritance, and it is divided into:

  • Upcasting → Casting child to parent (safe)
  • Downcasting → Casting parent to child (requires explicit cast, not always safe)

Casting plays a major role in polymorphism, method overriding, and runtime object handling.

2. Upcasting (Child → Parent)

Definition

Upcasting means assigning a child object to a parent reference.

Parent p = new Child(); // upcasting

Why Upcasting Is Safe?

  • A child is-a parent (IS-A relationship)
  • Child object contains everything the parent has
  • No explicit cast required

Example

class Animal {
    void sound() { System.out.println("Animal sound"); }
}

class Dog extends Animal {
    void bark() { System.out.println("Bark"); }
    @Override
    void sound() { System.out.println("Dog sound"); }
}

public class Main {
    public static void main(String[] args) {
        Animal a = new Dog(); // upcasting
        a.sound();            // Dog sound (polymorphism)
    }
}

Upcasting allows runtime polymorphism — the child method overrides the parent method.

Limitations of Upcasting

Animal a = new Dog();
a.bark();   // ERROR: bark() not accessible

Because the reference type controls what you can access.

inheritance

3. Downcasting (Parent → Child)

Definition

Downcasting means assigning a parent reference to a child reference.

Child c = (Child) p; // downcasting

Important:

  • Requires explicit cast
  • Not always safe
  • Can throw ClassCastException at runtime
  • Should only be done when the object is truly of the child type

Example

Animal a = new Dog(); // upcast
Dog d = (Dog) a;      // downcast (safe)
d.bark();             // works

Unsafe Downcasting Example

Animal a = new Animal();
Dog d = (Dog) a; // ERROR at runtime: ClassCastException

Because the actual object is not a Dog.

4. Checking Safe Downcasting Using instanceof

Before downcasting, always check:

if (a instanceof Dog) {
    Dog d = (Dog) a;
    d.bark();
}

Output:

Bark

This avoids runtime exceptions.

inheritance

5. Upcasting + Downcasting in Polymorphism

Example

Animal a = new Dog(); // upcasting
Dog d = (Dog) a;      // downcasting
d.bark();

Upcasting gives the object a parent reference; Downcasting restores the child-specific features.

6. Real-World Example

Example: Payment System

class Payment {
    void pay() {
        System.out.println("General payment");
    }
}

class CreditCard extends Payment {
    void creditOffer() {
        System.out.println("Special credit card offer");
    }
}

Usage:

Payment p = new CreditCard(); // upcasting
p.pay();

CreditCard c = (CreditCard) p; // downcasting
c.creditOffer();

7. Differences Between Upcasting and Downcasting

FeatureUpcastingDowncasting
DirectionChild → ParentParent → Child
Explicit cast required?NoYes
SafetyAlways safeNot always safe
AccessParent methods onlyChild methods too
PurposePolymorphismRegain child features

8. Summary

  • Upcasting is safe and automatic; enables polymorphism.
  • Downcasting is explicit and risky; must ensure type compatibility.
  • instanceof should be used to prevent ClassCastException.
  • Upcasting restricts access to parent-level features; downcasting restores child-specific methods.

This completes Upcasting and Downcasting in Java.

Written By: Shiva Srivastava

How is this guide?

Last updated on