views:

320

answers:

6

When can a certain object be cast into another object? Does the casted object have to be a subtype of the other object? I'm trying to figure out the rules...

Edit: I realized that I didn't explain my issue at all: basically I am casting an object to an interface type. However, at run-time, I get a java.lang.ClassCastException. What needs to happen with my object so that I can cast it to this interface? Does it have to implement it?

Thanks

+1  A: 

You can cast if the runtime type of an object is a subtype of what you're trying to cast it into.

EDIT:

Yes, the object that you're trying to cast will need to implement the interface in order for you to cast it successfully.

Anon.
You're thinking about this entirely the wrong way. What you should be asking is "in what situations does it make sense to treat this `Foo` as an `IBar`?
Anon.
what if the object I am trying to cast to that interface extends a class that implements the interface? will that avoid the run-time error?
alan
oh ok..i tried to rephrase my original comment since it didnt really make sense at first try
alan
+1  A: 

Take a look at Conversions and Promotions

adatapost
+4  A: 

In Java there are two types of reference variable casting:

  • Downcasting: If you have a reference variable that refers to a subtype object, you can assign it to a reference variable of the subtype. You must make an explicit cast to do this, and the result is that you can access the subtype's members with this new reference variable.

  • Upcasting: You can assign a reference variable to a supertype reference variable explicitly or implicitly. This is an inherently safe operation because the assignment restricts the access capabilities of the new variable.

Yes, you need to implement the interface directly or indirectly to enable assigning your class object reference to the interface type.

codaddict
A: 

If:

interface MyInterface{}

class MyClass implements MyInterface{}

Then

MyClass m = new MyClass();
MyInterface i = (MyInterface)m;

is possible.

Vincent Ramdhanie
A: 

There's an intuitive way of thinking about this - you're not changing an object with a cast, you're only doing something that would already be permitted if the type was known - inotherwords, you can only cast to a type that your object already is. So just look "up" the object chain to see what kinds apply to your object.

So you can cast to an interface only if it's defined somewhere higher up in the chain (e.g. if your classes parent implements it, etc. etc). It has to be explicit - from your question it sounds like you may be thinking that if you implement method "void foo()" then you should be able to cast to an interface that defines the method "void foo()" - this is sometimes described as "duck typing" (if it quacks like a duck, it's a duck) but is not how java works.

Steve B.
A: 

This will work:

class Foo implements Runnable {
    public void run() {}
}

Foo foo = new Foo();
System.out.println((Runnable) foo);

But this will not:

class Bar {
    public void run() {}
}

Bar bar = new Bar();
System.out.println((Runnable) bar);

Because although Bar has a run() method that could implement Runnable.run(), Bar is not declared to implement Runnable so it cannot be cast to Runnable.

Java requires that you declare implemented interfaces by name. It does not have duck typing, unlike some other languages such as Python and Go

finnw