简体   繁体   中英

Java rules for casting

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

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.

Suppose we want to cast d object to A,

A a = (C)d;

So internally 3 rules have been checked by Compiler and JVM. The compiler is checking first 2 rules at Compile time and JVM will check last one rule at Runtime.

Rule 1 (Compile time checking):

Type of 'd' and C must have some relation (child to parent or parent to child or same time).If there is no relationship then we will get a compile error(inconvertible types).

Rule 2 (Compile time checking):

'C' must be either same type or derived type(subclass) of 'A' otherwise we will get a compile error(incompatible types).

Rule 3 (Runtime Exception):

Runtime object type of 'd' must be same or derived a type of 'C' otherwise we will get a runtime exception (ClassCastException Exception).

Find following examples to get more idea,

String s = new String("hello"); StringBuffer sb = (StringBuffer)s;  // Compile error : Invertible types because there is no relationship between.

Object o = new String("hello"); StringBuffer sb = (String)o;       // Compile error : Incompatible types because String is not child class of StringBuffer.

Object o = new String("hello"); StringBuffer sb = (StringBuffer)o; // Runtime Exception : ClassCastException because 'o' is string type and trying to cast into StingBuffer and there is no relationship between String and StringBuffer.

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

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 (eg 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.

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.

If:

interface MyInterface{}

class MyClass implements MyInterface{}

Then

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

is possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM