🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.lang.java.help
3 messages
3 total messages Started by Shashank Sun, 08 Aug 1999 00:00
Help -- Java Error
#153
Author: Shashank
Date: Sun, 08 Aug 1999 00:00
50 lines
1478 bytes
Consider the following Java Program

class A

    int id;
    A()

       id = 1;
    }
    A(int i)

       id = i;
    }
    public int id()

       return id;
    }
}
class B extends A

    B(int i)

    }
}
public class FinalC

    public static void main(String[] args)

       A[] array = new B[5];
       array[0] = new B(10);
       array[1] = new B(8);
       array[2] = new B(6);
       array[3] = new A(4); // line 23
       array[4] = new A(2); // line 24
       for(int i = 0; i< 5; i++)

          System.out.println(array[i].id());
       }
    }
}


The execution returns the exception:

java.lang.ArrayStoreException:
        at FinalC.main(FinalC.java:23)

Why the program runs in this way.
What Changes should I make to the instructions on lines 23 and 24 to correct
the problem.
Re: Help -- Java Error
#154
Author: Patricia Shanahan
Date: Sun, 08 Aug 1999 00:00
71 lines
2462 bytes
Note that "{" got lost somewhere, I'm assuming they are where they
need to be.

The key problem is that you created an object of class array of B with
space for 5 elements. That means you can put references to B in it, or
references to objects of any class derived, directly or indirectly,
from B. You cannot put an A reference in it, because A is a superclass
of B, not a subclass.

A is a superclass of B, so any array of B references can only contain
references that could also be in an array of A references. It is O.K.
to use a variable of type A[] to refer to an object of class B[], but
like any object its class does not change.

If you want the array to contain a mixture of references to A and to
B, instantiate it as a A[5], not B[5].

Patricia


Shashank wrote:
> 
> Consider the following Java Program
> 
> class A
> 
>     int id;
>     A()
> 
>        id = 1;
>     }
>     A(int i)
> 
>        id = i;
>     }
>     public int id()
> 
>        return id;
>     }
> }
> class B extends A
> 
>     B(int i)
> 
>     }
> }
> public class FinalC
> 
>     public static void main(String[] args)
> 
>        A[] array = new B[5];
>        array[0] = new B(10);
>        array[1] = new B(8);
>        array[2] = new B(6);
>        array[3] = new A(4); // line 23
>        array[4] = new A(2); // line 24
>        for(int i = 0; i< 5; i++)
> 
>           System.out.println(array[i].id());
>        }
>     }
> }
> 
> The execution returns the exception:
> 
> java.lang.ArrayStoreException:
>         at FinalC.main(FinalC.java:23)
> 
> Why the program runs in this way.
> What Changes should I make to the instructions on lines 23 and 24 to correct
> the problem.
Re: Help -- Java Error
#155
Author: Alexander Berndt
Date: Mon, 09 Aug 1999 00:00
38 lines
1754 bytes
Shashank schrieb in Nachricht <7olh27$sb$1@news.chorus.net>...
>Consider the following Java Program


>class A
...
>class B extends A
...

>       A[] array = new B[5];
>       array[0] = new B(10);
>       array[1] = new B(8);
>       array[2] = new B(6);
>       array[3] = new A(4); // line 23
>       array[4] = new A(2); // line 24
>       for(int i = 0; i< 5; i++)

...
>java.lang.ArrayStoreException:
>        at FinalC.main(FinalC.java:23)


the error is some lines above.

A[] array = new B[5];

you have a declared A-array, but it is implemented as an B-array. So only B
and subclasses of B are allowed. On the other hand, you get only A-objects
out of this array.

A[] array = new A[5];
this helps, now you can insert A-objects and get-out A-objects.

To understand this behavior, you should remember that array's are just
special objects.

Ciao, Alex
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads