Friday, 26 February 2016

C#.NET ---- HIERARCHICAL INHERITANCE

                                                    HIERARCHICAL INHERITANCE 

Hierarchical inheritance is  one type of inheritance which is deriving two or more class from a single base class is know as hierarchical inheritance.

The same base class B1 properties are can share by different class d1, d2, d3...dn., are done here, and for each and every derived class a single copy is generated if we change the properties of a base class it cannot effect the other derived classes.


fig 1 :

fig 2: student is base class and art student , science student and commerce are derived class




A Sample example program on hierarchical inheritance ;

class Student
{
  public void print()
 {
   Console.Write("this   student ");
  }

}
class ArtsStudent : Student
{
  public void print1()
 {
    
   Console.WriteLine("is Arts student ");
  }
}
class ScienceStudent : Student
{
 public void print2()
 {
   Console.Write("is Science student ");
  }
}
class CommerceStudent : Student
{
 public void print3()
 {
   Console.Write("is Science student ");
  }
}

class Program
{
   ArtsStudent as = new ArtsStudent();   // creating object for each class 
   ScienceStudent ss = new ScienceStudent();
   CommerceStudent cs = new CommerceStudent();
   as.print();        // accessing base class method 
   as.print1();
   ss.print();
   ss.print2();
   cs.print();
   cs.print3();
    
     Console.ReadKey();
}

output is 
this student is Art student
this student is Science student 
this student is Commerce student


Program 2: another on hierarchical inheritance is here if you want to download it see below


No comments:

Post a Comment