- Home
- Interview Questions
- Java
- What happens when you cast a subclass object to a superclass…
Try answering first
In a real interview nobody shows you the answer. Think it through (or type it), then reveal the answers and rate yourself.
1 Answer
Short answer: Casting a subclass object to a superclass type is generally safe and allows accessing superclass properties but can lead to runtime exceptions if mismanaged.
Detailed explanation
In Java, when you cast a subclass object to a superclass type, you are promoting the object to a higher level of abstraction. This is known as upcasting and is always safe because every instance of a subclass is also an instance of its superclass. When you upcast, you can access only the members (fields and methods) of the superclass. However, if you attempt to downcast a superclass back to a subclass without checking the actual type of the object, this can lead to a ClassCastException at runtime.
Furthermore, to make downcasting safer, you can use the instanceof operator to check if the object you're casting is indeed an instance of the subclass you wish to cast to. For example, if you have a superclass Animal and subclasses Dog and Cat, you can safely cast a Dog object to Animal but should check whether an Animal variable is actually a Dog before downcasting.
Example
class Animal { } class Dog extends Animal { void bark() { System.out.println("Woof!"); } } public class Main { public static void main(String[] args) { Animal myAnimal = new Dog(); // Upcasting Dog dog = (Dog) myAnimal; // Downcasting dog.bark(); // Woof! } }Common mistakes
- Assuming downcasting is always safe without type checking.
- Not using
instanceofto safeguard againstClassCastException. - Confusing upcasting with downcasting in multi-level inheritance scenarios.
Follow-up questions
- Can downcasting lead to performance issues?
- What is the difference between implicit and explicit casting?
- How does polymorphism relate to type casting in Java?
Interview tip: Prepare examples to illustrate your understanding of upcasting and downcasting.
Reviewed by the Interview Questions editorial team · Last updated
How well did you know this?
Rate yourself to track progress, build your streak and get “didn't know” questions in your revision list.
Your Answer
More medium Java questions
- How does the class loader delegation model work in Java, and why does it exist? · 1 ans
- What are the constraints in Java generics and why are they used? · 1 ans
- What are the different types of garbage collectors in Java? · 1 ans
- What are the advantages of using interfaces in Java? · 1 ans
- What are static methods and when should you use them? · 1 ans
Related Questions
- What are the differences between primitive data types and wrapper classes? · Java · 1 ans
- What are static methods and when should you use them? · Java · 1 ans
- How do final variables differ from static final variables? · Java · 1 ans
- What are the advantages of using interfaces in Java? · Java · 1 ans
- How do abstract classes differ from interfaces in Java? · Java · 1 ans
- What are the different types of garbage collectors in Java? · Java · 1 ans
- How does the JVM manage memory for Java applications? · Java · 1 ans
- What are the constraints in Java generics and why are they used? · Java · 1 ans
🙋 Was this asked in your interview?
Which company? (optional — adds this question to the company's page)