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 2 of 3.
Dipendra said:
1 decade 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.
Everything from the base class is inherited to derived class.
Members marked private are not accessible to derived classes for integrity purpose.
Sunil said:
9 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.
So, 4+4() is base class and (4+4) is derived class.
Anju said:
7 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.
6 fields get inherited to the class even though two can't be used.
So 6*4 =24 bytes.
(1)
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.
Vivek sanjay shinde said:
2 years ago
There is a private variable in the parent class.
So the child class can contain only 5 variables.
So, 5*4 = 20bytes.
So the child class can contain only 5 variables.
So, 5*4 = 20bytes.
(1)
Narendra said:
7 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)
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 ?.
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.
Nithyapriya said:
1 decade ago
For integer 4 similarly double, float, char. What is the value for this and all?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers