File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array initialization problem

 
sandeep patrudu
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

What was the problem with code given below





class A { }
class B extends A { }
class C extends B { }


class l {




public static void main ( String args [ ] ) {
A [ ] x = new B [ 3 ] ;
x [ 0 ] = new A ( ) ; // ---> line 1
x [ 1 ] = new B ( ) ; // ---> line 2
x [ 2 ] = new C ( ) ; // ---> line 3
}

}


it is compiling but throwing exception at runtime.



Thanks,
sandeep
 
pete stein
Bartender
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though x was initially declared an array of A, it is initialized as an array of B. You can't store an "A" object in a "B" variable though you can store sub classes there such as C.
 
sandeep patrudu
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pete,
Thanks for helping on this.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic