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.
Vishwajeet Awasthi said:
1 decade 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:
First---
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.
First---
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.
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.
Jana said:
1 decade 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.
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.
Sakshi sharma said:
1 decade 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.
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.
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.
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.
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.
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.
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.
And complete memory that would be used is 12 + 12 byte = 24 byte.
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