0

We often end up using type casting in Java where we cast an Object of one to another in the same hierarchy.

I however could not find any material explaining how type casting worked implicitly.

Anyone here who could shed some light please.

Edit:

What I mean to ask is, given the code Superclass a = new Subclass(); // no cast needed Subclass b = (Subclass) a; // cast required

How does Java do the type casting from type SuperClass to SubClass.

While most posts say that Java automatically does it, I was a little curious to know what does it do automatically?

5
  • What do you mean by implicit type casting? Commented Oct 5, 2014 at 5:15
  • The only implicit casting for reference types (excluding Autoboxing on the assorted primitive wrappers) is "upcasting" which is really just application of the inversion of the Liskov substitution principle. Commented Oct 5, 2014 at 5:22
  • @user2864740 - Oops. My screen didn't refresh. Commented Oct 5, 2014 at 5:30
  • @Abhi <br/> some basics here 1) Super type references can always point to sub type objects 2) Java casting works at two levels: at compile time and at runtime <br/> You can see the complete details of this topic in JLS (docs.oracle.com/javase/specs/jls/se5.0/html/conversions.html) <br/> Also, look at Generics as to how it provides type safety. Commented Oct 5, 2014 at 5:49
  • You don't mean "implicit". Look the word up in a dictionary ... Commented Oct 5, 2014 at 7:09

1 Answer 1

3

For reference types, upcasting is implicit (or at least what I think you mean by implicit—that's not standard Java terminology). There is no implicit down-casting. So if you have Superclass base class and a Subclass class that extends Superclass, you can do this:

Subclass a = new Subclass();
Superclass b = a; // no cast needed.

However, for this:

Superclass a = new Subclass(); // no cast needed
Subclass b = (Subclass) a; // cast required

Note that in general you are at risk of throwing a ClassCastException when you downcast unless you can guarantee that the downcast will work. This is typically done by using the instanceof operator:

Superclass a = new Subclass(); // no cast needed
Subclass b;
if (a instanceof Subclass) {
    b = (Subclass) a; // cast required
} else {
    // handle problem of a being a different subtype of Superclass
}

The standard terminology for upcasting is widening and for downcasting it is narrowing the type.

EDIT (based on edit to question):

I don't know what those posts you cite mean, unless it's the following. In Java, all reference type variables are just that—references. All that's happening when Java executes:

Subclass b = (Subclass) a; // cast required

is that it is assigning the reference stored in a to b after first checking that the object referenced by a is assignment-compatible with the declared type of b. (Note that the above does not mean that the object is of the same type as b—it could be a subtype.) The object itself is not converted in any way to a new object structure. Any changes you make to the internals object through b will be seen if you then access the object through a (provided the changes affect the more abstract view of the object represented by a). In a sense, a and b are aliases (usually with different public apis) to the same object.

You will get a run-time error if the compatibility check fails. You will get a compile-time error if the check could not possibly succeed. Thus, you cannot get this past the compiler:

String x = "Hello world";
Integer y = (Integer) x; // compiler error

The aliasing described above does not happen when casting primitive values—such as from int to long. In those cases, Java will generate a new copy of the value in the machine representation of the cast type.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the terminology. I have edited the question to elaborate further. What I intended to ask was what does Java exactly do to cast one class type to another when it does Subclass b = (Subclass) a;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.