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 1 of 3.

Vivek sanjay shinde said:   1 year ago
There is a private variable in the parent class.

So the child class can contain only 5 variables.
So, 5*4 = 20bytes.
(1)

Narendra said:   6 years ago
The size of a derived class object is equal to the sum of sizes of data members in base class and the derived class.
(1)

Anju said:   6 years ago
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.
(1)

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.

Sridhar said:   6 years ago
Not getting this. Please explain in a correct way.

Sunil said:   8 years ago
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.

Vikas Verma said:   8 years ago
A size of int is 4.

How to check?

Console.WriteLine(sizeof(int));

Sol- 4

Raghupati Gupta said:   9 years ago
How will value 24 Byte?

Adi said:   9 years ago
How to check object memory?

Nithyapriya said:   9 years ago
For integer 4 similarly double, float, char. What is the value for this and all?


Post your comments here:

Your comments will be displayed after verification.