C# Programming - Inheritance - Discussion

Discussion Forum : Inheritance - General Questions (Q.No. 4)
4.
What will be the size of the object created by the following C#.NET code snippet?
namespace IndiabixConsoleApplication
{ 
    class Baseclass
    {
        private int i; 
        protected int j; 
        public int k;
    }
    class Derived: Baseclass
    {
        private int x; 
        protected int y; 
        public int z;
    }
    class MyProgram
    { 
        static void Main (string[ ] args)
        { 
            Derived d = new Derived();
        } 
    } 
}
24 bytes
12 bytes
20 bytes
10 bytes
16 bytes
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
29 comments Page 3 of 3.

Asish Kumar Satpathy said:   1 decade ago
In case of inheritance all the members of base class are inherited to derived class including "private" members. But the fact is that derived class can't access the private members although it have the private members.

Hence the size of derived class=base class+derived class itself.

=12bytes (base class) +12 bytes (derived class) =24 bytes.

Pushkaraj said:   1 decade ago
But Private variable should not be counted in the size of object.

Can you give brief detail about this ?.

Ncn said:   1 decade ago
Integer takes 4 bytes of memory.

Thus for 6 integer to be allocated memory will require 6*4=24 bytes.

Swathi said:   1 decade ago
Omprakash explained wrong way.

He counted 24 instead of 32.

Nandini Patil said:   1 decade ago
How can we check the size of objects?

Sunil Verma said:   1 decade ago
How can we check the size of the objects?

Mahdi said:   1 decade ago
Private fields of the parent are inherited to the child but they are inaccessible. So as Omprakash said, we have 4*6=24.

Omprakash said:   1 decade ago
According to how many variable are in given program.

Then add to its data type size of variable like 4+4+4+4+4+4+4+4=24 bytes.

Varun Aggarwal said:   1 decade ago
How can we check the size of the objects? please reply asap.


Post your comments here:

Your comments will be displayed after verification.