Wednesday 10 September 2014

Inheritance in c# with example

Inheritance:
One of the key concepts of Object Oriented Programming is inheritance. With inheritance, it is possible to create a new class from an existing one and add new features to it. Thus inheritance provides a mechanism for class level re-usability.

In Object Oriented Programming concept there are 3 types of Inheritance.

1. Single Inheritance,
2. Multiple Inheritance
3. Multilevel Inheritance


//Single Inheritance:
public class A
{
}
public class B : A
{

//Multiple Inheritance:
//Basically, C# does not support Multiple Inheritance, but we can achieve by
using Interfaces

public class A
{
}
public class B
{
}
public class C : A, B
{
}

//Multilevel Inheritance:
public class A
{
}
public class B : A
{
}
public class C : B
{
}

In the above three types C# don’t proved Multiple Inheritance. As there is conflict of multiple override methods in base classes (say A, B in above example)

As in Place C# give another feature called Interfaces
using interfaces you can achieve multiple Inheritance feature.


Ex:

public interface AX
{
}
public interface AY
{
}
public class LM : AX, AY
{
}


Interface does not contain any member definition, it contains only the signature.

0 comments:

Post a Comment