File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes object reference types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "object reference types" Watch "object reference types" New topic
Author

object reference types

veda vyas sista
Greenhorn

Joined: Sep 21, 2007
Posts: 10
ObjectRefConversion.java
1. public class ObjectRefConversion{
2. public static void main(String[] args) {
3. ClassRoom[] crooms = new ClassRoom[10];
4. Room[] rooms;
5. Lab[] labs;
6. for (int i = 0; i<5; i++) {
7. crooms[i] = new ClassRoom();
8. }
9. rooms = crooms;
10. labs = crooms; // compiler error
11. }
12. }
13.
14. interface Facilities {
15. }
16. class Room {
17. } .
18. class ClassRoom extends Room implements Facilities{
19. }
20. class Lab extends ClassRoom {
21. }


The conversion in line 9 will work because rooms and crooms are both arrays and the element type of crooms (that is, ClassRoom) is convertible to the element type of rooms (that is, Room). On the other hand, line 10 will generate a compiler error because the type of elements of array crooms (that is, ClassRoom) is not convertible to the type of elements of array labs (that is, Lab).



my doubt is why line 9 is not giving error but line 10 is giving error please explain me in detail
vivek kumar
Greenhorn

Joined: Sep 18, 2007
Posts: 13
These are not compatible:-
The hierarchy is like Room->ClassRoom->Lab
Rule is, super class array reference can hold the subclass array type but not the reverse.
So rooms = crooms; is correct but not labs = crooms;
vivek kumar
Greenhorn

Joined: Sep 18, 2007
Posts: 13
These are not compatible:-
The hierarchy is like Room->ClassRoom->Lab
Rule is, super class array reference can hold the subclass array type but not the reverse.
So rooms = crooms; is correct but not labs = crooms;
 
 
subject: object reference types
 
Similar Threads
Inheritence
Casting & Conversion of Objects
Seeking Exam Advise
Sun Cirtification
Problem with Array.