Downcasting 'Object' to an Interface.
I have a query regarding typecasting.
1)Any class that we create implicitly extends 'Object'
2) An interface does not extend 'Object'
Then,how can an 'Object' be typecasted to an Interface type
when an interface is not in the inheritance heirarchy.?
Look at the code below:
The i.next() returns an Object which is type casted to an interface.
Is this possible?
HashMap hm =new HashMap();
Set set = hm.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry = (Map.Entry)i.next();-- How can an Object
-- be typecasted to an Interface?
}
> I have a query regarding typecasting.
>
> 1)Any class that we create implicitly extends
> 'Object'
> 2) An interface does not extend 'Object'
>
> Then,how can an 'Object' be typecasted to an
> Interface type
> when an interface is not in the inheritance
> heirarchy.?
Because all you to is change the type of a reference - there is no syntactical problem about casting a reference type X to an unrelated type Y.
> Then,how can an 'Object' be typecasted to an> Interface type> when an interface is not in the inheritance> heirarchy.?Because something must implement that interface, and that implementation must extend Object.Kaj
kajbja at 2007-7-13 19:22:44 >

An interface is a type, but you can only create instances of (non-abstract) classes. Thus the exact type of any object you encounter is of some class, wthether it implements an interface or not.
The static type (that is, the one you declare a variable in the source) can be an interface, but every object is of the type of some (non-abstract) class.
> Because all you to is change the type of a reference
> - there is no syntactical problem about
> casting a reference type X to an unrelated type Y.
I take that back. The compiler does check for an inheritance relationship. But as BIJ said, references can only hold real objects, and every object extends Object.
An interface is a subtype of the type Object - or at any rate theJLS 4.10.2 says of an interface type with no direct superinterfaces thatits direct supertype is the type Obejct.
Also, [url http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5]JLS 5.5 Casting Conversion[/url] says:
The detailed rules for compile-time legality of a casting conversion of a value of compile-time reference type S to a compile-time reference type T are as follows:
If S is a class type:
...
If T is an interface type:
If S is not a final class (?.1.1), then, if there exists a supertype X of T, and a supertype Y of S, such that both X and Y are provably distinct parameterized types, and that the erasures of X and Y are the same, a compile-time error occurs. Otherwise, the cast is always legal at compile time (because even if S does not implement T, a subclass of S might).
jverda at 2007-7-13 19:22:44 >

> (because even if S does not implement T, a subclass of S might).Best reply so far.
BIJ001 is pretty much spot on with his description. You have compile time type, class of the object and runtime type. The compile time type of a variable is, obviously, known at compile time and can be an interface type. However since you never have an instance of an interface any object you encounter has a class associated with it, this is called the class of the object. The runtime type is the class of the object you are referring to at runtime.
As for you question, the code you posted will not actually compile since you need to do something like:
Map.Entry m = (Map.Entry) i.next();
Here m has a compile time type of Map.Entry, but the class of the object is Object hence why you can type cast successfully.
YoGeea at 2007-7-13 19:22:44 >

... oh and of course the fact that anything you add to a Map "is a" Entry else you would get a ClassCastException.
YoGeea at 2007-7-13 19:22:44 >

> An interface is a type, but you can only create
> instances of (non-abstract) classes. Thus the exact
> type of any object you encounter is of some class,
> wthether it implements an interface or not.
>
> The static type (that is, the one you declare a
> variable in the source) can be an interface, but
> every object is of the type of some (non-abstract)
> class.
I am having one doubt about what 'type' mean? Pls explain in detail.
> I am having one doubt about what 'type' mean? Pls> explain in detail.Type: the class or instance that defines the reference.
List list = new ArrayList();The bold part is the type of the reference list.
If I have my variable called 'a' what is type?
> If I have my variable called 'a' what is type?How the h3ll should we know, its your variable!
> If I have my variable called 'a' what is type?Look in your source where you declare 'a' - look to the left of 'a' - that is your type.
I will understand 'type' now. Thank you.
> I will understand 'type' now. Thank you.One small step for a man, another giant leap forward for Sanjeev's downgrade to JDK1.1.1.