views:

139

answers:

3

If I have classes A, B, C, D, E, and interfaces like X, Y, Z, and model a system like:

class B : A, X
class C : B, Y
class D : C, Z
class E : D

If A is an abstract base class and E is the class of interest, when I create an instance of E, would it in turn create instances of A, B, C, D, X, Y, Z in addition to E?

If that's the case, would this create a huge performance overhead? Not memory, but runtime and GC wise.

+6  A: 

Yes, it would create 'embedded' instances of A, B, C and D
No, it would not create instances of X, Y and Z (because they are interfaces)

There is no extra overhead for the memory allocation or GC (of ABCD) because the instance of E is allocated as 1 block. Any runtime overhead would entirely depend on the constructors involved.

There will always be a chain of contructors (from E to A) being executed, possibly the default constructor but it's also possible to call multiple constructors at 1 level.

Henk Holterman
Sorry, but there are no instances created by inheritance !
Obalix
@Obalix,note the quotes around embedded. But yes, there ultimately is only 1 instance involved.
Henk Holterman
Thanks Henk, so in that case, for instance if you were to merge these classes into a single class with no inheritance, would they pretty much be the same in terms of memory, GC and runtime performance?
Joan Venge
@Joan: Yes. ___
Henk Holterman
+2  A: 

The inheritance extends the Type and does not create instances. You have one single instance of E that includes the data defined by A, B, C, D and E. It provides methods and property accessors defined by these classes and the interfaces X, Y, and Z.

Obalix
+5  A: 

It would create a single object - an instance of E - but that would include all the fields declared in the class hierarchy. (Interfaces can't declare fields, so they're irrelevant to the data within the object itself.) It's only fields (and any attributes affecting layout, admittedly) that contribute to the memory taken up by an object.

The reference to the instance of E could be "converted" to a reference of type A, B, C, D, X, Y or Z as an identity-preserving reference conversion - i.e. it would still be a reference to the same object.

Jon Skeet
Thanks Jon. When you convert E into any of the above, would it create a new instance? I think it does for base classes, right? But for interfaces?
Joan Venge
Also for instance if you were to merge these classes into a single class with no inheritance, would they pretty much be the same in terms of memory, GC and runtime performance?
Joan Venge
@Joan: You do not convert the instance of E, only the reference to an instance is 'converted', which is very cheap.
Henk Holterman
Thanks Henk, how does it work? You mean the reference pointer is converted to another type? Actually I thought all reference pointers were the same, i.e. didn't have info about what it points too.
Joan Venge
@Joan: at compile time it very much has information about what it points to, even in unmanaged languages. .NET extends that to the runtime, the CLR verifies correct conversion. But there is no physical conversion.
Henk Holterman
Thanks Henk, in that case, is it some sort of mechanism ("the conversion") that does nothing more than restricting the way you access the members? Like you can't access members outside of A, when you "convert" E into A?
Joan Venge
@Joan: Yes again.
Henk Holterman