R N REDDY .Net Community - C#
Sunday, 2016-12-04, 11:49 PM
Welcome Guest | RSS
Site menu
Tag Board
500
Our poll
Rate my site
Total of answers: 144
Statistics

Total online: 1
Guests: 1
Users: 0


31-12-2010



Inheritance: Deriving the features from one class to another class can be called as inheritance.

Inheriting the base class members to derived class or inheriting the super class members into subclass pr inheriting the parent class members into child class can be called as inheritance.

To implement inheritance we will use colon ( : ) operator.

Syntax for inheritance:
    <Accessmodifier> class <baseclassname>
   {
   // class members
    }
   <Accessmodifier> class <Derivedclassname> : <baseclassname>
   {
   // class members
    }


When we will implement the inheritance all the base calss members will be logically inheriting to derived class.

Because of the inheritance derived class object can access the base calss members.

In inheritance can be classified into various types.

1. Single Inheritance: Inheriting form one base class to one derived class can be called as single inheritance.

Eg:
Class C1
 {

 }
Class C2:C1
 {

 }

Eg For single Inheritance:
namespace singleinheritanceexample
 {
   Public bc
    {
      Public int a = 10;
      Public int b = 20;
      Public void bc method()
      {
        Console.WriteLine("bc method is calling");
      }
    }
   Public class dc:bc
   {
    Public int c = 30;
    Public void dcmethod()
     {
       Console.WriteLine("dc method is calling");
     }
  }

 Class mainclass
  {
    Void main()
      {
        dc d = new dc();
        Console.WriteLine("a value is {0}, b value is {1}", d.a, d.b);
        d.bcmethod();
        Console.WriteLine("C value is " +d.c);
        d.dcmethod();
        Console.ReadLine();
      }
  }
}
Output: a value is 10, b value is 20
             bc method is calling
             c value is 30
            dc method is calling

2. Multilevel Inheritance: Inheriting from one class to another class from that class to some other class can be called as multilevel inheritance

Eg:
Class C1              Class GF
{                          {
}                          }
Class C2:C1        Clas Father:GF
{                          {
}                          }
Class C3:C2        Class Child:Father
{                          {
}                          }

Eg: For Multilevel inheritance:
namespace multipleinheritanceexample
{
  Public class bc
    {
      Public int a = 10;
      Public int b = 20;
      Public void bcmethod()
       {
         Console.WriteLine("bc method is calling");
       }
    }
   Public class dc:bc
    {
      Public int c = 30;
      Public void dcmethod()
        {
          Console.WriteLine("dc method is calling");
        }
    }
   Public class tc:dc
     {
        Public void tcmethod()
          {
             Console.WriteLine("tcmethod is calling");
          }
     }

   Class mainclass
    {
       Void main()
         {
           tc t = new tc();
           Console.WriteLine("a value is {0}, b value is {1}", t.a, t.b);
           t.bcmethod();
           Console.WriteLine("C value is " +t.c);
           t.dcmethod();
           t.tcmethod();
           Console.ReadLine();
         }
     }
 }
Output: a value is 10, b value is 20
              bc method is calling
              c value is 30
              dc method is calling
               tc method is calling

3. Multiple Inheritance: Inheriting from multiple super classes to one derived classs can be called as multiple inheritance.
EG:
Class C1                   Class Fatherbrother
{                               {
}                               }
Class C2                   Clas Father
{                               {           
}                               }
Class C3:C1,C2        Class Child:Father,Fatherbrother
{                              {
}                               }

In C#.NET by using classes we cannot implement multiple inheritance, we can achieve multiple inheritance by using interfaces.

Sealed Class: While declaring class it used sealed keyword the class can be called as sealed class.

Sealed classes cannot be inherited.

Whenever we want to restrict a class to be inherited we can declare that class as a sealed class.

Eg: For Sealed Class:
Public sealed Class bc
{
}
Public class dc:bc
{
}

The above code will generate an error because we cannot inherit scaled classes.



Search

Time
 

 

Calendar
 
«  December 2016  »
SuMoTuWeThFrSa
    123
45678910
11121314151617
18192021222324
25262728293031

Twitter
 

Entries archive

Copyright R N Reddy © 2016Developed by Nagendra, Venkat, Vijaya and Yaswanth
Get free updates as SMS to your mobile. Just register by clicking here