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?

}

[782 byte] By [bhuru_luthriaa] at [2007-10-2 18:03:39]
# 1

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

CeciNEstPasUnProgrammeura at 2007-7-13 19:22:44 > top of Java-index,Java Essentials,Java Programming...
# 2
> 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 > top of Java-index,Java Essentials,Java Programming...
# 3

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.

BIJ001a at 2007-7-13 19:22:44 > top of Java-index,Java Essentials,Java Programming...
# 4

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

CeciNEstPasUnProgrammeura at 2007-7-13 19:22:44 > top of Java-index,Java Essentials,Java Programming...
# 5
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.
pbrockway2a at 2007-7-13 19:22:44 > top of Java-index,Java Essentials,Java Programming...
# 6

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 > top of Java-index,Java Essentials,Java Programming...
# 7
> (because even if S does not implement T, a subclass of S might).Best reply so far.
CeciNEstPasUnProgrammeura at 2007-7-13 19:22:44 > top of Java-index,Java Essentials,Java Programming...
# 8

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 > top of Java-index,Java Essentials,Java Programming...
# 9
... 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 > top of Java-index,Java Essentials,Java Programming...
# 10

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

sanjeev_bokmanajia at 2007-7-13 19:22:44 > top of Java-index,Java Essentials,Java Programming...
# 11
> I am having one doubt about what 'type' mean? Pls> explain in detail.Type: the class or instance that defines the reference.
CeciNEstPasUnProgrammeura at 2007-7-13 19:22:44 > top of Java-index,Java Essentials,Java Programming...
# 12
List list = new ArrayList();The bold part is the type of the reference list.
CeciNEstPasUnProgrammeura at 2007-7-13 19:22:44 > top of Java-index,Java Essentials,Java Programming...
# 13
If I have my variable called 'a' what is type?
sanjeev_bokmanajia at 2007-7-13 19:22:44 > top of Java-index,Java Essentials,Java Programming...
# 14
> If I have my variable called 'a' what is type?How the h3ll should we know, its your variable!
ScarletPimpernela at 2007-7-13 19:22:44 > top of Java-index,Java Essentials,Java Programming...
# 15
> 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.
tschodta at 2007-7-20 23:30:31 > top of Java-index,Java Essentials,Java Programming...
# 16
I will understand 'type' now. Thank you.
sanjeev_bokmanajia at 2007-7-20 23:30:31 > top of Java-index,Java Essentials,Java Programming...
# 17
> 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.
ScarletPimpernela at 2007-7-20 23:30:31 > top of Java-index,Java Essentials,Java Programming...