Up/Down Casting & instanceof
Understand upcasting to a parent type, safe downcasting with instanceof, and common pitfalls that cause ClassCastException.
Up/Down Casting & instanceof is a free Java Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Up vs down casting
Upcasting stores a child object in a parent type reference and is always safe. Downcasting goes the other way and needs care. We use instanceof to check the real type at runtime.
Code: upcasting demo
Upcasting is implicit and safe: a Dog can be treated as an Animal. You can call methods declared on Animal; the Dog version runs at runtime.
public class Main {
static class Animal {
void speak() { System.out.println("Animal sound"); }
}
static class Dog extends Animal {
void speak() { System.out.println("Woof"); }
void fetch() { System.out.println("Fetch!"); }
}
public static void main(String[] args) {
// Upcasting: Dog stored in an Animal reference (safe, automatic)
Animal a = new Dog();
a.speak(); // calls Dog's speak() due to dynamic binding
// a.fetch(); // Not allowed: reference is Animal, which does not declare fetch
System.out.println("Upcasting hides Dog-only methods from the reference type.");
}
}
Why downcast?
You downcast when you need a child-only method like fetch(). This should be rare and well-checked. Prefer writing code against the parent or an interface when possible.
Code: safe downcasting
Use instanceof before casting. If the check fails, skip the cast and choose another path; this avoids ClassCastException.
public class Main {
static class Animal { void speak() { System.out.println("Animal sound"); } }
static class Dog extends Animal { void speak() { System.out.println("Woof"); } void fetch() { System.out.println("Fetch!"); } }
static class Cat extends Animal { void speak() { System.out.println("Meow"); } }
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
// Safe downcast using instanceof
if (a1 instanceof Dog) {
Dog d = (Dog) a1;
d.fetch(); // OK
}
if (a2 instanceof Dog) {
Dog d2 = (Dog) a2; // will not run
d2.fetch();
} else {
System.out.println("a2 is not a Dog, skip casting.");
}
}
}
Common pitfalls
Pitfall: casting without checking can throw ClassCastException. Another pitfall is overusing downcasts instead of designing to interfaces. Keep casts close to where you need them and prefer polymorphism.
Code: bad cast vs safe cast
Never cast blindly. If you are not sure of the runtime type, guard the cast with instanceof and pick a safe alternative if it does not match.
public class Main {
static class Animal { void speak() { System.out.println("Animal"); } }
static class Dog extends Animal { void fetch() { System.out.println("Fetch!"); } }
static class Cat extends Animal { }
public static void main(String[] args) {
Animal a = new Cat();
// Bad: direct cast (would throw at runtime)
// Dog dBad = (Dog) a; // ClassCastException if uncommented
// Good: check first
if (a instanceof Dog) {
Dog d = (Dog) a;
d.fetch();
} else {
System.out.println("Not a Dog; skipping cast.");
}
}
}
Downcast check
Quick check: How do you safely downcast an Animal reference to Dog?
Recap
Recap: Upcasting is safe and common. Downcasting requires instanceof to avoid crashes; prefer designing to parent types or interfaces to reduce casts.
Frequently asked questions
Is the “Up/Down Casting & instanceof” lesson free?
Yes — the full text of “Up/Down Casting & instanceof” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Up/Down Casting & instanceof”?
Understand upcasting to a parent type, safe downcasting with instanceof, and common pitfalls that cause ClassCastException. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Up/Down Casting & instanceof” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Abstract Classes
- Interfaces (intro & contrasts)
- Up/Down Casting & instanceof