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();
}
}
}
Discussion:
29 comments Page 1 of 3.
Nileema said:
1 decade ago
Can anybody explain it?
Varun Aggarwal said:
1 decade ago
How can we check the size of the objects? please reply asap.
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.
Then add to its data type size of variable like 4+4+4+4+4+4+4+4=24 bytes.
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.
Sunil Verma said:
1 decade ago
How can we check the size of the objects?
Nandini Patil said:
1 decade ago
How can we check the size of objects?
Swathi said:
1 decade ago
Omprakash explained wrong way.
He counted 24 instead of 32.
He counted 24 instead of 32.
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.
Thus for 6 integer to be allocated memory will require 6*4=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 ?.
Can you give brief detail about this ?.
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.
Hence the size of derived class=base class+derived class itself.
=12bytes (base class) +12 bytes (derived class) =24 bytes.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers