Inheritance and its Rules and Regulation in c sharp

Inheritance


  • It is a mechanism of consuming the member that are defined in one class from another class which provides reusability.
  • Once we apply inheritance b/w two classes child classes can consume the member of its parent class as it is the owner of those member except private member.

Syntax


Example

Add a class "class1.cs"and write the following code 


Add a "class2.cs" and write the following code






Rules and Regulation that has to adopted while working with inheritance

  • In inheritance parent class constructor should always be accessible to child class or else inheritance will not be possible. The reason behind this is wherever the instance of a child class created it implicitly called its parent classes constructor for execution. So if at all the parent class constructor is not accessible to the child class inheritance will not be possible.
Note :   The reason why parent class constructor is called is, Inheritance is all about consuming parent class member from child class so to consume the parent member from child class first the parent class variables has to initialized and to initialized the variables of a class the constructor of class only is responsible. So when the child class instance is created child class constructor will implicitly called its parent classes constructor so that the parent class variable are initialized.
  • In inheritance child classes can consume the members of it parent class but a parent class can never consume the member of the child class that are purely defined under the child class.
  • Just like we can initialized the variable of a class by using the instance of the class, we can also initialized the variables of a parent class with the help of its child classes instances.                                                            Class2 c = new Class2();                                                                                                                  Class1 p = c;                                                                                             In this case the parent class is consuming the object memory of the child classes instance.         
In the above case even if p is accessing the same object memory of its child class now also by using the parents reference we  can’t call any member of the child class that is  purely defined under the child class.

we can never initialized a child class variable by using by its parent classes instance, but with the help of parent class reference that is created by using child class reference we can make it as child class reference.

To convent the parents reference back into child reference we need to perform an explicit conversion.
  
                                    Class2 c = new Class2 ();
                                    Class1 p = c;
Converting the parent's reference back to child's reference .

                                   Class2 obj = (Class2) p ;

Now, By using the child reference we can all member of parent class as well as child class.

  • Each and every class that is predefined or user defined has a default parent class that are the class object of system namespace so the member of this class can be accessed from any class those are Equals () , GetHashCode (), GetType (); ToString ().
Note : Object is first class that is defined under the base class library providing low level service to all its child classes and this class is the ultimate base class for all the predefined and user defined classes.




 

No comments:

Post a Comment