Denys_newbie
Denys_newbie

Reputation: 1160

How I created objects of an abstract class?

  1. Why GeometricObject[] abstractObjects = new GeometricObject[10]; don't cause error that I create objects of an abstract class?
  2. What's the difference between 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

Answers (1)

Rahul Kumar
Rahul Kumar

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

Related Questions