Reputation: 137
I'm trying to clarify this so I fully understand type casting. Please correct me on anything that is incorrect as I've been self learning java for about 2 months now at a really slow pace.
Let's say I created a class called SubObject. And I am aware that all classes that do not have a direct explicit superclass, are assumed to be subclasses of Object class.
Object obj1 = new SubObject();
SubObject subObj1 = (SubObject) obj1;
System.out.println(subObj1); //prints out com.examplePackage.SubObject1234e1234;
So I have successfully downcasted the reference of the base class (Object) to its derived class (subObject). However...
Object obj2 = new Object();
SubObject subObj2 = (SubObject) obj2;//this throws the ClassCastException error.
My understanding of ClassCastException error is that it's inherited RuntimeException to catch it during compile time, to show that the code has attempted to cast an object to a subclass of which it is not an instance. Because subObj2 is NOT an instance of SubObject, but rather Object, it is incompatible.
So I have 2 questions: 1. Is there any flaw/error in my understanding? 2. In which situation is downcasting actually used? Thanks for the help everyone.
Upvotes: 1
Views: 1164
Reputation: 500
A Situation in which Downcasting could occur is in the context of the factory design pattern. In this design pattern the implementation details of multiple child classes can be abstracted by just using their parent class.
Consider the following example:
import java.util.ArrayList;
public class Factory {
private ArrayList<Product> products = new ArrayList<Product>();
public static void main(String[] args){
Factory factory = new Factory();
factory.addProduct(new Ball("1"));
factory.addProduct(new ToyCar("2"));
factory.addProduct(new Dice("3"));
Product popProduct = factory.popProduct();
Ball downcastBall = (Ball)popProduct;
System.out.println(downcastBall.getId());
}
public void addProduct(Product prodIn){
products.add(prodIn);
}
public Product popProduct(){
Product returnProd = products.get(0);
products.remove(0);
return returnProd;
}
}
The classes Ball, ToyCar and Dice all extend the Product class and implement the getId() method of it, so the three children can all be treated as just Products. This is called a polymorphism.
I Post the rest of the code so you can play around with it.
The Ball class:
public class Ball extends Product{
public Ball(String id) {
super(id);
}
public String getId(){
return "ball!";
}
}
The ToyCar class:
public class ToyCar extends Product{
private String ToyCarId;
public ToyCar(String id) {
super(id);
}
public String getId(){
return this.ToyCarId;
}
}
The Dice class:
public class Dice extends Product{
private String diceId;
public Dice(String id) {
super(id);
}
public String getId(){
return this.diceId;
}
}
And finally the Product class:
public class Product {
private String id;
public Product(String id){
this.id = id;
}
public String getId(){
return this.id;
}
}
Upvotes: 0
Reputation: 16130
The relevance of RuntimeException
is that it 'unchecked'. So a developer is not forced to deal with it. The normal approach to this is to check (using an if
statement and using something like instanceOf
) to see if the cast is allowed before allowing the cast.
So,
if(subObj2 instanceof SubObject) {
SubObject s2 = (SubObject) obj2;
...
}
However, this is considered a 'code smell' -- i.e. something that is unusual and which rings alarm bells in the minds of senior developers. Generally you should be achieving this sort of thing through polymorphism, though that's not always possible or desirable. For most types of programs, type casting should not be necessary most of the time.
However, your thinking is broadly correct.
Upvotes: 1