The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

oops property...

Posted by G.Salai Sivamal on June 04, 2001 at 7:05 AM

> hi
> i made a class like this
> class a extends Object implements Runnable
> {
> public static void main(String s[])
> {
> a a1=new a();
> Object o=a1;
> Runnable r=o;
> }}
> however this does not compiler as it asks for a explicit cast
> however changing the program as
> Runnable r=a1;
> Object o=r;
> does not require any cast .please explain why.
> gopal

You cannot do downcasting in java. Means, you cannot assign a parent object to child object without doing explicit casting.
thats why you getting error..

since Object is the root of all the jdk classes. the Runnble was inherited from the Object some how..so it is not compilling.

do it like, it will compile
Runnable r=(Runnable)o;

regs
Salai Sivamal.G






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us