Reputation: 1160
GeometricObject[] abstractObjects = new GeometricObject[10];
don't cause error that I create objects of an abstract class?A[] objects;
and A[] objects = new A[7];
public class Test
{
public static void main(String[] args)
{
int[] numbers = new int[8];
A[] objects = new A[7];
GeometricObject[] abstractObjects = new GeometricObject[10];
System.out.println(numbers[3]); // prints 0
System.out.println(objects[4]); // prints null
System.out.println(abstractObjects[6]); // prints null
}
}
abstract class GeometricObject {}
class A
{
public int number;
A()
{
number = 13;
}
}
Upvotes: 1
Views: 45
Reputation: 372
GeometricObject[] abstractObjects = new GeometricObject[10];
you are not creating object of GeometricObject here.
You are creating an object of an array(of type GeometricObject class
).
What's the difference between A[] objects; and A[] objects = new A[7];
A[] objects = new A[2]
is divided in two parts
A[] objects
- its called deceleration of object.
new A[7]
- called initialization of object
Upvotes: 3