Author Topic:   Object Reference Casting
Shiva
ranch hand
posted April 08, 2000 09:36 PM         
can we cast an interface to a non final class?
according to SRH p.117(1,3)
Old Type is an interface, new Type is a Non final class
it says " Always OK" but when i try to run this it says
Incompatible Type
am i missing something,

Thanks in advance

code:

public interface Myinterface{

public void dosomething();
}

class MyClass implements Myinterface{

public void dosomething(){
//
}
}

class Another extends MyClass{

public void dosomething(){
//
}
}

class Q12{
public static void main(String [] args){

Myinterface Mi1;// = new Myinterface();
// OK interfaces cant be instantiated
MyClass mcl = new MyClass();
Another acl = new Another();

Mi1 = mcl;
Mi1 = acl;

mcl = (MyClass) Mi1;//doesnt work without cast
acl = (Another) Mi1;//same here

}
}


------------------


[This message has been edited by maha anna (edited April 08, 2000).]

[This message has been edited by maha anna (edited April 10, 2000).]

maha anna
bartender
posted April 10, 2000 11:39 AM             
Shiva,
Any non-final normal class can be casted and assigned to a ref of an Interface. As long as you put cast it correctly it is Always OK . I think you are thinking about 'conversion'. For conversion, the right hand side (oldtype) MUST implement the interface. You have to know the difference between 'casting' and 'conversion'.

When we say

NewType nt;
OldType ot;
nt = (NewType)ot;

when NewType is an interface and OldType is a non-final normal class, the above statement is always Ok. Because even if this OldType class doesn't implement the NewType interface at present, some day, some programmer, in future may decide to create a subclass of this 'non-final' class and implemnt this interface right? Which means this ref to ot may very well be refer to an object of the 'future' subclass and everything will be fine. The Java compiler thinks like this exactly and says 'Always Ok' at 'Compile time'. But remember that , at run time the 'true face' will be revealed. . A 'ClasscastException' WILL be thrown if the actual physical object DOESN'T in face implemet this interface.

Also note that the table in R&H at page 117 is Compile time Rules for Object reference Casting

Is this clear to you now Shiva?
regds
maha anna

[This message has been edited by maha anna (edited April 10, 2000).]

Shiva
ranch hand
posted April 10, 2000 01:28 PM         
Hi Maha,
i mixed up the words i should have said
"Reference Assignment Conversions"
when i hunt for some other references found this,
if old type is an interface type, oldRef can be assigned to
newRef provided newRef type is Object//**
this creates a new querry in my mind
if you can assign an interface type to an Object type
MyInterface M1;
Object obRef = new Object();
obRef = M1;// ok cauz its superclass of all types
but if i create
class MyClass implements MyInterface{
//
}

MyClass mcl = new MyClass();
mcl = M1;// i need explicit (cast) to convert interface type to class type. MYclass must have descended from Object also its implementing the interface then why do i need compulsory cast
just to satisfy the compiler? or is it always needed

interface
|
class type

so when you upcast its ok and when you downcast need explicit cast(does this rule applies here)

i think i need to goback to Thinking in java again"

Regards,

Shiva


------------------

maha anna
bartender
posted April 10, 2000 02:58 PM             
Shiva,
if old type is an interface type, oldRef can be assigned to newRef provided newRef type is Object

- is exactly true. In fact it can be assigned ONLY to an object of type Object.

- In java, all ref type (class/interface/array) ALL decend from Object class.
- class ref further creates its own class hirearchy by means of subclasses.
- But interfaces can be extended by ONLY interfaces like the foll.
interface inter1 {}
interface inter2 externds inter1 {}
- And also interfaces can be implemented by ANY class.
- So from the interface ref var itself, you CAN NOT say, it refers to which class implements it. (maximum you can generalize to Object class)
_ This is the reason why you CANNOT convert just like that a ref of type Interface to ANY class except Object.
- THis is the same reason, why the compiler allows when you just cast the interface ref to ANY class.

- And also there is no hirerarchy idea of if a class implement an interface then , the interface ref can be converted to the class. Actuaally it is the other way around. If a class implemnents an interface then the a reference to an object which is of that particular class type CAN BE CONVERTED to the interface which the class implements. In other words,
MyInterface mt;
class MyClass implements MyInterface {}
MyClass mc = new MyClass();
mt = mc; // this is ok
mc = mt; //THis is not ok. Because from the ref to an interface itself we can't say that the typr of the class which implements it, since interface is common to all classes and can be implemented by any class provided it has the access level.
Also note that If you take a class there is a SINGLE class hirearchy from the root Object. But for interfaces there is no single class hirearchy. It is scattered. Also for a class you can say whether it is of a particular type or not confidently by seeing the hirearchy. But not for interfaces. Since ANY class can implement interfaces.

I stop here itself. Now I leave you to explore Thinking in Java.
regds
maha anna

[This message has been edited by maha anna (edited April 10, 2000).]

Shiva
ranch hand
posted April 10, 2000 06:27 PM         
Thank you so much Maha anna for your detail information,
it cleared my doubts about interface

------------------

|