views:

218

answers:

2

When you derive from a class and instance the subclass, the runtime also instances the super class, right?

Since abstract classes can't be instanced, are they not created by the runtime when a subclass is instanced?

If so, then abstract class inheritance would be faster than normal class instance?

+6  A: 

The runtime never creates separate instances of the base class and the derived class - it's just that the derived class instance also has all the variables etc of the base class, and runs the base class constructor as part of initialization. There's no difference here between "normal" base classes and abstract base classes.

Jon Skeet
+5  A: 

I think you have some details confused.

When you construct an object, where the class of that object inherits from another class, it's not like you get two objects in memory.

You only get one, but it has space set aside for fields that comes from both.

Put another way, if the original class needs 10 bytes to hold its fields, and your inherited class needs 5 bytes to hold its specific fields, when you construct an object from your inherited class, it would occupy 15 bytes, where the first 10 corresponds to the fields from the base class. (note, this is a very simplified explanation, there's a lot more going on that dictates the actual size of objects).

Lasse V. Karlsen