Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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

Object cast to Object[]

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why Object can be casted to Object[] but other Class type can not
be casted to array of that class?
Object o = new Object();
Object[] ob = (Object[])o;
MyClass c = new MyClass();
MyClass c[] =(MyClass[]) c;
Howard
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because an Object[] is (polymorphically) an Object (everything is polymorphically an object), whereas MyClass[] is not polymorphically a class. The compiler does not check what variable o is pointing to; it just knows that it is (polymorphically) an Object. So, when you attempt to cast it to an Object[] the compiler "says" to itself" "Well, o just *might* be referring to an Object[], so let's accept it, and wait till run-time to find out.
Hope this helps,
Panagiotis.

Originally posted by Howard Hu:
why Object can be casted to Object[] but other Class type can not
be casted to array of that class?

Object o = new Object();
Object[] ob = (Object[])o;

MyClass c = new MyClass();
MyClass c[] =(MyClass[]) c;

Howard


 
Howard Hu
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Panajiotis,
Yes, it makes sense. Here is another question related to this.
Object o = new int[10];
Base class Object refers to an array. So now we are able to use subclass's overidding methods and subclass-added methods. What are those method that the object o here can use? What is that subclass definition for an array?
--Howard
 
Panagiotis Varlagas
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every array type (e.g. int[]) is considered to be a subtype of Object. You can invoke any method of Object on o (toString(), hashCode(), getClass() etc.). Also, every array type adds a public final field (length) to what it inherits from Object.
You cannot extend an array type yourself. The only way to do so is to extend the component class. (e.g. MyString[] is polymorhically a String[]). However, this would not add *any* functionality to the array type itself.
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic