• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

array an object ??

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is the following givin a ompiler error as
public class t1 {
staticObject x;
public static void main(String h[])
{
Object a[] = x;
}
}
t1.java:10: Incompatible type for declaration. Explicit cast needed to convert java.lang.Object to java.lang.Object[].
Object a[] = x;
is array not an object ?? pls explain
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ravi,
*) In Java arrays ARE implemented as Objects.
*) But you are trying to convert an array to an array of Objects.
*) We can convert the objects up the inheritance hierarchy.
The inheritance hierarchy is as follows(from Khalid's book)
<pre>

java.lang.Object
|
--------------------------------
| |
int[] Object[]

</pre>
So, the array of objects inherits from the object class directly.
static Object x;
Object a[] = x;
Here you are converting down the inheritance hierarchy(as seen in figure above).
So the compiler is trying to explain about this.
Hope this helps.

------------------
Regards
---------
vadiraj

*****************
There's a lot of I in J.
*****************
[This message has been edited by vadiraj vd (edited January 10, 2001).]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !
This is Golu_Jain.My friend you have to know 2 things :-

1.>'Object' class is the super class of all the classes and is also the superclass for 'Object[]'.
2.>You can not convert the superclass object to the subclass without explicit casting.

I hope that you are clear now.
GOLU
 
I've never won anything before. Not even a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic