简体   繁体   中英

Couple java questions regarding type casting and inheritance

Hello I have a few questions regarding type casting and inheritance. I have been doing some reading and I understand the point and basics of type casting. However, I don't fully understand where I can and can't use it.

Consider this class:

class A{

        public A(){}            

}

A temp = new A();

temp = (Object)temp;

This code gives me the error "Cannot convert from type Object to type A". However, wouldn't this be converting from type A to type Object? Can you not type cast up the hierarchy?

Now my second question regards inheritance and such.

When you type:

Object temp = new A();

what is really happening? Is temp an A or is it an Object?

Here the excerpt from JLS 8.1.3 :

If the class declaration for any other class has no extends clause, then the class has the class Object as its implicit direct superclass.

Of course, Object itself is a bit special ( JLS ):

Each class except Object is an extension of (that is, a subclass of) a single existing class (§8.1.3) and may implement interfaces (§8.1.4).

Every Class is a descendant of Object .

In your case, you are trying store an object of A into a Class object called A . This isn't going to work. You need to do:

Object testObject = (Object)temp;

This will store the Object into testObject , which has the type Object that you casted to.

Here it is working on ideone .

A a = (Object)temp;

"Cannot convert from type Object to type A". However, wouldn't this be converting from type A to type Object? Can you not type cast up the hierarchy?

You are correct that

(Object)temp;

is converting temp , which is an A to an Object . However, that's not what the compiler is complaining about. Now that you have, effectively,

A a = anObjectNOTAnA

( A = ... is invalid code. I've changed it to A a = ... .)

It's saying that you cannot convert an Object back to an A , unless you explicitely cast it and potentially suppress the unchecked-cast warning:

A a = (A)anObjectNOTAnA

or

@SuppressWarnings("unchecked")
A a = (A)anObjectNOTAnA

Regarding your other question:

Object temp = new A();

What is really happening? Is temp an A or is it an Object?

When you cast an object of any type, it never changes the actual type of the underlying object. A new A() is always an A , whether its

A a = new A();

or

Object objButReallyA = new A();

or

@SuppressWarnings("unchecked")
A a = (A)((Object)new A());

If an A is stored in an Object , it's just a different "view" of the same object. In order to use the A specific functions, however, you must first revert it back to the A view:

 objButReallyA.getAOnlyField();  //compile error

 ((A)objButReallyA).getAOnlyField();  //Okay

It's just because you can't assign superclass object to the subclass reference.

So you can't do:

temp = (Object)temp;

because it's the same as:

Object newObject = new Object();
A temp = newObject;

You will get the same compile error here.

Of course you can do something like that:

Object newObject = new Object;
A temp = new A();
newObject = temp;

You can do it because you can assign subclass to the superclass reference

The problem is in the last line. First you promise Java that temp is of type Object by the statement:

(Object) temp

Afterwards you try to assign an object that the compiler thinks is of type Object to a variable that should be of type A. So to conclude, the part where you cast temp to type Object is fine, the problem is when you afterwards try to assign it to a variable expecting something of type A.

For your second question, temp is an A. When creating an object with the new keyword, the type of the object will always be whatever comes right after. In your case A. Afterwards you then assign temp to a variable of type Object, but that does not change the actual type of temp. Having a variable of some type X just tells you that whatever the variable is pointing to is a subtype of X.

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