Hello,
I am a beginner. Inheritance is somehow hard to understand.
Example Code:
class zzz
{
public static void Main()
{
xxx a = new xxx();
a.abc();
}
}
class yyy
{
public int i = 10;
public void abc()
{
System.Console.WriteLine("yyy abc");
}
public void pqr()
{
System.Console.WriteLine("yyy pqr");
}
}
class xxx : yyy
{
}
Q.1) xxx class is derived from the yyy class. Are the codes that belongs to the parent class(yyy) copied or inserted to the derived class(xxx) while compiling these codes? Or is the abc() method just a reference to the abc() method in yyy?
Q.2) If the codes are copied to the derived class from the parent class, why does the constructor of yyy class get called?
Q.3) Why is the constructor of yyy get called before the xxx's? Where and when do we use this?
Thanks.
AlanPosted Nov 26, 2008, 6:38 AM
As inheritance is one of the pillars of object oriented programming (and hence of C#), I thought it might be helpful for me to expand a bit on Munir's answers:
Q.1) The first thing to note about inheritance is that a derived class inherits all its parent's class's members, except its constructor(s) and destructor (if it has one). It even inherits 'private' members despite the fact that they won't be directly accessible from within the derived class.
So, you can think of it as though all the parent class's members (including any inherited from its parent in turn) had been copied into the derived class's code. In reality, they're not copied but it may be helpful for you to think of it in this way.
However, a better analogy is to think of a derived class and its ancestors as being like the layers of an onion. At the centre is System.Object (the ultimate parent of all .NET classes) and then each ancestor adds a layer of members until the derived class itself adds the outer layer.
Q.2) Because constructors are not inherited but may contain code which initializes the parent class's fields, they have to be called from the derived class's constructor. This can be done in one of two ways:
1. By calling them explicitly using the : base(...) syntax.
2. If you don't use the syntax in #1, the system automatically calls the parent class's parameterless constructor, provided it has one and it's not private. So, if the parent class doesn't have such a constructor, you must use the syntax in #1, otherwise you'll get a compiler error.
Q.3) The parent class' constructors are called in a chain up to System.Object's constructor. They are called before the actual code in the constructor is executed. As the stack unwinds, the actual code then executes in reverse order to which the constructors were called.
In your example this means that System.Object's constructor executes first, then yyy's constructor (as it doesn't have one the system provides a public parameterless one which does nothing) and finally xxx's constructor (again provided by the system).
This is the natural order of doing things because initialization of a derived class may depend on members inherited from its parent class.
In the interest of generality, the system always follows these steps even though some of the constructors may do nothing except call their base class's constructor.
Munir ShaikhPosted Nov 26, 2008, 5:36 AM
First of all thanks for such questions, these questions will get asked in interviews though they are very basic
Q.1) xxx class is derived from the yyy class. Are the codes that belongs to the parent class(yyy) copied or inserted to the derived class(xxx) while compiling these codes? Or is the abc() method just a reference to the abc() method in yyy?
Ans:While compiling the code yyy class will get act as BaseType to the xxx class so it is just a reference
Q.2) If the codes are copied to the derived class from the parent class, why does the constructor of yyy class get called?
Ans:No code get copied to the yyy class (i have created library application on the above mentioned classes and used reflection to get the code so it is for sure)
Q.3) Why is the constructor of yyy get called before the xxx's? Where and when do we use this?
Ans: xxx is acting as child class so it has to look into BaseType yyy and which will tell what method to be inherited so the base constructor get called first
Enjoy!!
>>Munnamax