Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+23 votes

Q: In what situation will casting (TypeA) objB cause an error?
I would assume when TypeB's constructor is private because if the TypeB's constructor is private, you wouldn't even be able to create an object of TypeB in the first place. Would there be any other situations where it might cause an error other than when a constructor of TypeB is private?

asked in CSC305 Fall 2022 by (1 point)

3 Answers

+9 votes
 
Best answer

1) First of all, if TypeB's constructor is private, then you would get an error EARLIER - wherever you try to create a new instance of TypeB. This is unrelated to the question of when you can cast objectB to TypeA.

2) The key idea here is about compatible types for casting objects. Let's assume that objB has an actual type of TypeB. Then we can legally cast (TypeA) objB if TypeA is a superclass of TypeB (or a superclass of a superclass, etc), or TypeA is an interface that TypeB implements. This is sometimes called "upcasting", since we would be casting the object to a class or interface that is higher up (following the up arrows) in an UML diagram.

3) Another very important situation to consider, though, is that objB's declared type might be different from its actual type. For example, in JavaFX, BorderPane is a subclass of Pane which is a subclass of Region which is a subclass of Parent, which is a subclass of Node. Suppose at some point we wrote:

Node obj = new BorderPane();

Then obj's actual type is BorderPane, even though the declared type is Node.

  • Is it legal to cast (BorderPane) obj?
    • Yes, because that is the obj variable's actual type.
  • Is it legal to cast (Parent) obj?
    • Yes, because the actual type of obj is BorderPane, and Parent is an ancestor (superclass of superclass of superclass...) of BorderPane.
  • Is it legal to cast (Object) obj?
    • Yes. Every class descends from Object.
  • Is it legal to cast (AnchorPane) obj?
    • No, because obj's real type is BorderPane, and a BorderPane is not a descendant of AnchorPane.
  • Is it legal to cast (Styleable) obj?
    • Yes, because BorderPane implements the Styleable interface, and we can declare variables as interface types as well.

If you're still feeling confused, maybe try reading through this tutorial:
https://www.baeldung.com/java-type-casting

answered by (5.7k points)
+2

Great answer, makes a lot of sense.

+13 votes

Originally we have TypeA objectA = new TypeB(); which means TypeB is a subclass of TypeA

So casting (TypeA) objectB causes error when you're trying to access the methods and properties of TypeB for objectB, but objectB isn't instance of TypeB, it will give a run-time error (class cast exception).

Since objectB is declared as TypeA, it can only access properties and methods of TypeA. When objectB isn't TypeB, it couldn't cast to recover methods from TypeB.
That's how it works for casting relationship between superclass and subclass.

answered by (1 point)
+12 votes

In addition to Stephanie's answer, someone else goes into more detail about how casting works behind the scenes here:
https://stackoverflow.com/questions/13405377/how-does-java-object-casting-work-behind-the-scene
They talk about how if you try to cast an Object as a Child: "Child b = (Child) new Object();" that will give you an error, but if you did the opposite, casting a Child as an object, that would work because a child is an object, but an object isn't a child.

answered by (1 point)
...