# Creating arrays of object of abstract class in statically-typed object-oriented languages

In statically-typed object-oriented languages (like Java, C++, C#, ...), suppose I declare an abstract class:

public abstract class myclass1 {
.................
}


Most of these languages (like Java) wouldn't allow us to create an Object of an Abstract class, but they do allow us create an Array of Object of an Abstract class. Like,

myclass1 arr[] = new myclass1[10];


We can create an array of 10 objects.

I have some confusion about how this kind of array works. How do I use this array? What is the actual use of this array in real-life projects? Any explanation with an example will be very useful.

• I guess it's important to note that in such languages, you'll (always, at least in Java) get an array of references to objects that are stored elsewhere. Hence, in memory arrays of (references to) objects all look the same; the type is merely a tool for the programmers and their tools (in particular, the compiler) to make sure method calls are valid (during type checking).
– Raphael
Sep 8 '15 at 22:22
• Also, do you understand the use of interfaces? In particulare, given interface I { ... }, you see the use of an array of type I[]? And, separately, the use of inheritance? If you can answer these three questions with "yes", then just combine them to explain the use of abstract classes. If there is a "no", that's the question you really want to ask, and it's not an issue specific to abstract classes.
– Raphael
Sep 8 '15 at 22:24
• @Raphael, thanks: yes, Wandering Logic's edit seems to me like it helps -- thank you to Wandering Logic! The edit makes it clearer to me that this is not a question about Java but about statically-typed object-oriented languages, so I've re-opened -- others can make up their own minds about the revised question.
– D.W.
Sep 8 '15 at 23:54

In statically-typed object oriented languages (like Java), you can not create an object of an abstract class. Abstract classes are (usually) not completely defined, so creating such an object wouldn't make any sense.

What you can create is a reference to an object instance of an abstract class. A reference is just a pointer to an object, not the object itself.

myclass1 arr[] = new myclass1[10];


Creates an array of 10 null references to objects of class myclass1.

Similarly

myclass1 x = null;


Creates a copy of the null reference and casts it to a reference to a myclass1.

You can always assign an object of a (non-abstract) subclass of myclass1 to a reference to myclass1. So for example:

x = new ConcreteClass();


(where ConcreteClass is a subclass of myclass1).

Any element of arr is also a reference to myclass1. So:

arr[7] = new ConcreteClass();

• "an object belonging to an abstract class" -- shouldn't we say, "an object of the type induced by the abstract class"? This may be a nitpick, but I honestly don't know what "object o belongs to class C" would mean in general. I know what "o is an instance of C" and "o has type T" mean, though.
– Raphael
Sep 8 '15 at 22:20