简体   繁体   中英

What is the difference between object of an abstract class and list of objects of abstract class?

We can't create objects of an abstract class, but we can create a List or an array of them. What is the difference?

A list or array is simply a place holder for a set of pointers, and you have NOT created instances of anything yet.

When you say Create Object - you mean create an instance - which you cannot do with an abstract class.

But you can create Lists or Arrays that point to them (and are EMPTY) - then you can move the pointers to 'real' instances of derived classes/objects

Summary of Answer:

The List or the array itself is not an abstract class, even if the type it declares it will hold is an abstract class.

More Detailed Answer:

Even though you can't make an object of an abstract class (the word "of" is somewhat vague, but obviously you mean as a direct instantiation of that class), you can make an object that is an instance of an abstract class (because it's an instance of a concrete class that extends said abstract class).

Basically, given abstract class Abstraction , and concrete class Concrete that extends Abstraction , List<Abstraction> means you can but anything in it that is an instance of something that extends Abstraction , eg an instance of Concrete . Although arrays are a bit different in some ways from Lists, the same goes for them as well.

Example:

Given this class:

public abstract class Abstraction {

}

And this class:

public class Concrete : Abstraction {

}

You can't do this:

Abstraction a = new Abstraction();

But you CAN do this:

Abstraction a = new Concrete();

And then you can do this:

IList<Abstraction> list = new List();
list.add(a);

It is possible to declare and initialise a list where the type of the members is an abstract class because to create this object you do not need to actually instantiate the abstract class.

Logically, the list is only a container for pointers.

For example, you can define a list of an abstract class, but this list can be at the beginning empty.

You can also initialise the list with an initial size, but at the beginning it will not contain references to actual objects: the initialisation will only preallocate space for the pointers.

The objects added to the list have to be instances of a concrete class that extends the abstract class.

You can't create an instance of an abstract class for obvious reasons but surely creating an instance of the List<T> class is allowed regardless that T is an abstract class.

Let the abstract class be called ClassAbstract .

  • When you try to do - new ClassAbstract() , you try to create an object of the ClassAbstract .
  • When you do - new List<ClassAbstract>() , you create an object of the List<T> class.

The List is empty !

That means no instance of the abstract class.

Create an empty list is allowed. You can (later) add to it a non abstract class which extends it.

I believe u are simply strongly typing what the objects inside the array will be

So when it comes time to put instantiated objects into the array. It has to be an object that was extended from the abstract class

There are no pink magic unicorns.

But you can still build a stable for pink magic unicorns, you just won't ever be able to put any in there, because there aren't any.

More realistic analogy: you cannot go to the pet store and buy "a mammal". That's far too abstract a thing.

But, you can build a stable for mammals. And then you can buy horses, cows, dogs, cats, elephants, giraffes, wolves, leopards, and lions and put them in there. Maybe even pink magic unicorns, if you can find any.

(How to build a stable that can at the same time accommodate mice and hump whales is left as an exercise to the reader.)

No, you can't create an array "of them", because you can't put the abstract types into the list. A list of an abstract type holds any derived instances of the abstract type.

Just means any derived type of the abstract type can be in the list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM