How can an instance of a base class contain an instance of a derived class?

I have been a .Net programmer (can't say I'm a programmer) for 2 years. There is one question that I cannot understand for many years, since an instance of a base class can contain an instance of a derived class?

Suppose we have two classes:

class BaseClass { public A propertyA; public B propertyB; } class DerivedClass :BaseClass { public C propertyC; } 

How could this happen:

 BaseClass obj = new DerivedClass () 

I mean, the BaseClass memory BaseClass does not have a place for the newly added C property, so how could it hold the value of the C property?

On the other hand, how this cannot happen:

 DerivedClass obj = new BaseClass() 

I thought this was the right way, since the DerivedClass memory DerivedClass has all the spaces for BaseClass and even more. But this is not so, why?

I know I'm asking a really stupid question, but can someone give me a more detailed answer? That would be better in terms of memory or compiler.

+8
inheritance oop derived-class
source share
5 answers

Because (and this concept is often misunderstood) a variable containing a pointer is introduced regardless of the specific concrete type of object it points to.

So when you write

 BaseClass obj = new DerivedClass () 

the part that says BaseClass obj creates a new link variable on the stack, which the compiler understands to contain a link to what is or is happening from BaseClass .

The part that reads = new DerivedClass () actually creates a new object of type DerivedClass , stores it on the heap, and stores a pointer to this object in the memory location, where obj points to. The actual object on the heap is DerivedClass ; this is called a specific type of object.

A variable obj type BaseClass . This is not a specific type, but simply a variable type that restricts the compiler from storing an address in a variable pointing to an object that is not BaseClass or which is not the result of a BaseClass type.

This is to ensure that everything you specify in the obj variable, regardless of whether it is a specific BaseClass or DerivedClass , will have all methods, properties, and other elements of type BaseClass . He tells the compiler that all uses of the obj variable should be limited to using only those members of the BaseClass class.

+5
source share

Since BaseClass is a reference type, obj not a BaseClass object. This is a reference to a BaseClass object. In particular, this is a reference to the BaseClass part of DerivedClass .

+4
source share

When you say this:

 BaseClass obj = new DerivedClass () 

You DO NOT say, “Create a container to contain this BaseClass, and then insert this larger DerivedClass into it.”

In fact, you are creating a DerivedClass object, and it is being created in a memory space sufficient for the DerivedClass object. The .NET Framework never monitors the fact that this thing is, in particular, DerivedClass, and it never ceases to consider it as such.

However, when you say that you want to use the BaseClass object variable, you simply create a link / pointer to this object that has already been created and selected and defined, and all this, and your pointer is a bit more vague,

It's like this guy, on the other side of the room, is a red-eyed Irish slightly overloaded chicken farmer with bad teeth and a charming personality named Jimmy, but you just refer to him as “that dude.” The fact that you are vaguely describing him , does not change what it is, or any of its details, although your vague description is absolutely accurate.

+2
source share

The answer copied verbatim from the fact that another user, jk, wrote the exact same question here in Stackoverflow.

If I tell you that I have a dog, you can safely assume that I have a dog.

If I tell you that I have a pet, you don’t know if this animal is a dog, it can be a cat, or maybe even a giraffe. Not knowing the additional information that you cannot safely assume that I have a dog.

similarly, a derived object is an object of a base class (like its subclass class), so you can point to it with a pointer to the base class. however, the base class object is not a derived class object, so it cannot be assigned to a derived class.

(The creaking you hear now is a stretch of analogy)

Suppose you now want to buy me a present for your pet.

In the first scenario, you know that this is a dog, you can buy me a leash, everyone is happy.

In the second scenario, I did not tell you what my pet is, if you are going to buy me a gift, somehow you need to know the information that I do not have (or just guessed), you will buy me a leash if it turns out that I indeed we had a dog, everyone is happy.

However, if I really had a cat, then we now know that you made a bad guess (casting) and have a miserable cat on a leash (runtime error)

0
source share

This is possible due to the way memory is allocated for the base and derived classes. The derived class first lists the instance variables of the base class, followed by the instance variables of the derived class. When a reference variable of a base class is assigned to an object of a derived class, it sees the instance variables of the base class that it expects, plus the instance variables of the derived class "extra".

0
source share

All Articles