Can we store any class object as array elements in object type arrays?
class Array18
{
public static void main(String[] args)
{
Number[] n=new Number[5];
n[0]=new Integer(10);
n[1]=new Double(35.6);
n[2]=new String("altaf");
for(Number a:n)
System.out.println(a);
}
}
/*
Output:- CE[incompatible types]
Explanation:- For object type arrays, we can store either declared type object or its child
class object as array elements. Here String is not child class of Number class.
*/
{
public static void main(String[] args)
{
Number[] n=new Number[5];
n[0]=new Integer(10);
n[1]=new Double(35.6);
n[2]=new String("altaf");
for(Number a:n)
System.out.println(a);
}
}
/*
Output:- CE[incompatible types]
Explanation:- For object type arrays, we can store either declared type object or its child
class object as array elements. Here String is not child class of Number class.
*/
0 comments: