Wednesday 19 August 2015

What is an inheritance in .Net with Examples

  Defination:        
                      Acquiring the properties of  base class into the derived class is called as inheritance.
Syntax: 
              [<modifiers>] class <cname> : <parentclassname>


eg:                               class class1
                                     {
                                        ---- members-----
                                      }
                                       class class2 : class1


There are Two type of Inheritance : 
                                                     As for the standards of Object-oriented programming There are two types Inheritance.

  1. Single Inheritance
  2. Multiple Inheritance
Single Inheritance : 
                                    If a class has only on immediate parent class to the it's known as Single inheritance.


                       class 1---------->class2-------->class3--------->class4


 Multiple Inheritance :
                                 If a class has more than one immediate parent class to it . It's known as Multiple Inheritance.


                                      class1                       class2
                                           |                                   |
                                           |-------> class3 <--- |


Note :
                Inheritance allows to consume members of the parent under child. But not private members. Default scope for members of a class was private only.


----> Add a class inherit.cs write the following 


                using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
    class inherit
    {
        public inherit()
        {
            Console.WriteLine("inherit constructor");
        }
        public void test1()
        {
            Console.WriteLine("method one");
        }
        public void test2()
        {
            Console.WriteLine("method two");
        }


    }
}

-----> Add a new  class inherit1.cs write the following 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class inherit1 : inherit 
    {
        public inherit1()
        {
            Console.WriteLine("inherit1 constructor");
        }
        public void test3()
        {
            Console.WriteLine("Method three");
        }
        static void Main()
        {
            inherit1 i = new inherit1();
            i.test1();
            i.test2();
            i.test3();
            Console.ReadLine();
        }
    }
}

Output : 

Explanation of the Inheritance :
  •    In Inheritance execution of a child class always starts by invoking the default constructor of parent class. Which should be accessible to the child. It not accessible inheritance will not be possible.
  • Members of the parent class can be accessed by child classes where as child class members can't be accessed by parent class. Because a parent class is not aware it is child classes.
                      We test this write the following code under the child class main method.
                                 inherit p = new inherit(); // here p is parent object.
                                   p.test1();  p.test2();  
                                   p.test3();  // here test3() is child class method can't be accessible. i.e Invalid
  •   AS we aware that object of a class can be assigned to the variable of the same class. It can also be assigned to variable of it's parent class and make it was a reference . By using that reference we can't invoke members of child class. 
                      To test this rewrite the code under Main method of child class inherit1 as following.

                                 inherit1 i = new inherit1();
                                   inherit1 p =i; // p is not object . this is reference to child class.
                                   p.test1();
                                   p.test2();
                                   p.test3();     // invalid

Note : the  object of the child class that is present as parent 's reference. If required can be converted by into child's reference. But should be done by using explicit type casting.
                            inherit1 i = new inherit1();
                               inherit p = i;;
                              inherit1 i1 = (inherit1)p;
                                           or 
                              inherit1 i1 = p as inherit1;
                                i1.test1(); i1.test2(); i1.test3();
Note : using child reference we can assign call all the methods.
  • Every class what we define in .Net languages has a default parent class. Even if we inheritance or not. i.e the class object. when we compile a program a compiler verify whether the class is inheriting from another class. If not inheriting compiler inherits from the class object .So object class will be a parent either directly or indirectly for any class.
                          object----------->inherit-------------> inherit1
                   The member of object class can be accessed from any class what we are going define.
 eg:       GetType();    ToSting() etc

 -----> To check this rewrite the code under main method of class to as following
               
                  object obj = new object();
                  Console.WriteLine(obj.GetType());
                   inherit p = new inherit();
                   Console.WriteLine(p.GetType());
                   inherit1 c = new inherit1();
                   Console.WriteLine(c.GetType());


Another Example of Inheritance : 

parent class :

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class inherit
    {
        public inherit()
        {
            Console.WriteLine("inherit constructor");
        }
        public void test1()
        {
            Console.WriteLine("method one");
        }
        public void test2()
        {
            Console.WriteLine("method two");
        }

    }
}
---------------------------------------------------------------------------------
Child Class :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class inherit1 : inherit 
    {
        static  inherit1()
        {
            Console.WriteLine("inherit1 constructor");
        }
        public void test3()
        {
            Console.WriteLine("Method three");
        }
        static void Main()
        {
            inherit1 i = new inherit1();
            i.test1();
            i.test2();
            i.test3();
            Console.ReadLine();
        }
    }
}

Output :

Explanation :
        
                   Here Execution of the class starts from Static Constructor method  of any parent or child class. If no Static Constructor  then execution starts from Default Constructor of the parent class can executed. In a class Static Constructor or Static Main() should be there executed. 

Some Steps for Execution Starts from :
  1. Static Constructor Method    Note :  // here parent  or child class constructor first executed . If both constructor are Static . First Execute the Child Class Static Constructor and Static Main method (if any methods in Main() method).
  2. Parent class Methods.
  3. Child class   Methods.

1 comment: