Reputation: 3665
I have class BaseObject and InheritedObject inherites from BaseObject.
In BaseObject, I have field "public int Id". In InheritedObject, I have field "public new long Id".
Question: When new instance of InheritedObject is created, are inherited object and base object stored in 2 memory space/block?
For more specific, I have address 0x001 to store the BaseObject and address 0x100 to store the InheritedObject . And InheritedObject has reference to BaseObject using address 0x001.
Upvotes: 1
Views: 475
Reputation: 73472
Assuming c#, Yes two discrete fields are created. So two memory space will be allocated.
When you add new
modifier, you shadow the base class's field. So when you access the field with derived type you'll get the value from InheritedObject
and to get the value of BaseObject.Id
you can access it via base object type.
Note: You can change the data type also when shadowing in derived class, so it is obvious that separate memory location is used.
Upvotes: 2