C# Programming - Inheritance - Discussion

Discussion Forum : 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();
24 bytes
12 bytes
20 bytes
10 bytes
16 bytes
Answer: Option
No answer description is available. Let's discuss.
29 comments Page 2 of 3.

Adarsh Pandey said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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:   1 decade ago
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:   10 years ago
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:   10 years ago
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:   10 years ago
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:   10 years ago
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:   9 years ago
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. :).

Post your comments here:

Your comments will be displayed after verification.