C# Programming - Inheritance - Discussion

Discussion :: Inheritance - General Questions (Q.No.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();

[A]. 24 bytes
[B]. 12 bytes
[C]. 20 bytes
[D]. 10 bytes
[E]. 16 bytes

Answer: Option A


No answer description available for this question.

Nileema said: (Jun 6, 2011)  
Can anybody explain it?

Varun Aggarwal said: (Jun 10, 2011)  
How can we check the size of the objects? please reply asap.

Omprakash said: (Jul 15, 2011)  
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.

Mahdi said: (Sep 15, 2011)  
Private fields of the parent are inherited to the child but they are inaccessible. So as Omprakash said, we have 4*6=24.

Sunil Verma said: (Sep 17, 2011)  
How can we check the size of the objects?

Nandini Patil said: (Sep 26, 2011)  
How can we check the size of objects?

Swathi said: (Sep 28, 2011)  
Omprakash explained wrong way.

He counted 24 instead of 32.

Ncn said: (Dec 15, 2011)  
Integer takes 4 bytes of memory.

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

Pushkaraj said: (Mar 30, 2012)  
But Private variable should not be counted in the size of object.

Can you give brief detail about this ?.

Asish Kumar Satpathy said: (Jul 23, 2012)  
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.

Adarsh Pandey said: (Apr 5, 2013)  
When we create child class object, Firstly Parent Class object is created internally and hence all the variable in that class has got memory. Total no.of int variable=3 so 12 byte memory is allocated as one int variable take 4 byte memory and then child class object is created and all the variable of child class got memory again there are 3 int type variable so total memory =12 byte.

And complete memory that would be used is 12 + 12 byte = 24 byte.

Prashant Kumar Sharma said: (Sep 8, 2013)  
If we inherited any class then all the member variable, it may be public, private, protected are inherite in the derived class so in this class 6 int variable are there and 1 int store in the 4 bytes so total memory required is 6*4= 24 bytes.

Bharat said: (Oct 30, 2013)  
It's Integer not float to take size 4 bytes.

Integer size if 2 bytes so it take it 2 bytes my answer is 10 bytes.

Because when it inherits base class into derived class only protected member and public member its inherited to derived class.

Shankar said: (Dec 1, 2013)  
There are three variables of type int in base class and in derived class. The object created is of derived class (which has inherited base class). We know that the size of int = 4bytes. Since we have 6 variables now (including the base class) 24 bytes memory is allocated to the object.

Kush Kumar said: (Jan 30, 2014)  
Because even if base class consisting any accessibility modifier to its data field, all data field is going to be inherited to the child class (including private field also) , but base class private field can not access inside child class.

Sakshi Sharma said: (Jul 28, 2014)  
Integer takes 2 bytes of memory and when base class is inherited into the derived class then only protected and public members gets inherited so as a result 10 bytes should be the answer to this question.

Jana said: (Aug 13, 2014)  
Whenever you are creating object for derived class, you need to consolidate the size of each and every variable existing in both parent and child classes for getting size of object.

You need to include the size of the private variables existing in parent class, because in inheritance you can get the details of private variables from parent class also.

Dipendra said: (Aug 18, 2014)  
Guys the answer is 24 bytes.

Everything from the base class is inherited to derived class.

Members marked private are not accessible to derived classes for integrity purpose.

Vishwajeet Awasthi said: (Nov 27, 2014)  
If you make object of derived class then first the object of base class will be instantiated then after that the object of derive class will be instantiated. So in this case:

Class BaseClass
private int i; (4 bytes)
protected int j; (4 bytes)
public int k; (4 bytes)

=12 bytes.

Class BaseClass
private int i; (4 bytes)
protected int j; (4 bytes)
public int k; (4 bytes)

=12 bytes.

And as I explained earlier if derived class is instantiated then automatically the object of base class is instantiated firstly. So, 12+12 = 24 bytes.

Shelja said: (Mar 10, 2015)  
Each integer (int) type data requires 4 bytes since in this program total 6 int data type variables are given.

Hence we can calculate the required memory for object by multiplying number of int variable and the 4 bytes memory required by each variable that is 6*4=24. :).

Nithyapriya said: (Jun 12, 2015)  
For integer 4 similarly double, float, char. What is the value for this and all?

Adi said: (Sep 6, 2015)  
How to check object memory?

Raghupati Gupta said: (Oct 31, 2015)  
How will value 24 Byte?

Vikas Verma said: (Apr 17, 2016)  
A size of int is 4.

How to check?


Sol- 4

Sunil said: (Jun 2, 2016)  
But we cannot access the protected member in the object of derived class. So memory does not occupy by this.

So, 4+4() is base class and (4+4) is derived class.

Anju said: (May 19, 2018)  
By default, int is int32 in C# which takes 4 bytes.

6 fields get inherited to the class even though two can't be used.

So 6*4 =24 bytes.

Sridhar said: (Jun 10, 2018)  
Not getting this. Please explain in a correct way.

Narendra said: (Aug 22, 2018)  
The size of a derived class object is equal to the sum of sizes of data members in base class and the derived class.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.