Tuesday, 14 February 2012

Inheritance

Inheritance:

Getting the features of one class to another class is known as inheritance.

The class which is giving features to another class is known as base class or parent class.

The class which getting the features from another class is known as derived class or child class.

Data members and methods are called as features of class.

The main advantage of inheritance is ‘code reusability’.

Types of inheritance are

1) Single inheritance

2) Multilevel inheritance

3) Hierarchical inheritance

4) Multiple inheritance

5) Hybrid inheritance

Single inheritance:

In this inheritance, there is only one base class and one derived class.

Multilevel inheritance:

In this we have many base classes and many derived classes. The classes which are acts as a base class as well as derived class known as intermediate classes.

Hierarchical inheritance:

In this, there is no. of derived classes derived from one base class.

Multiple inheritance:

In this, one class derived from multiple base classes. Multiple inheritance is not supported in c# in classes. We can achieve this by interfaces.

Hybrid inheritance:

The combination of any two inheritances is known as hybrid inheritance.


Example:

public class A

{

public void Add()

{

console.writeline("this is class A's method");

}

}

public class B : A

{

public void print ()

{

console.writeline ("this is class B's method");

}

class program

{

static void main(string[] args)

{

A x = new A ();

x.print();

B x= new B ();

x.print ();

}

}

No comments:

Post a Comment