|
Inheritance is one of the primary concepts of object-oriented programming. Inheritance is the process by which we can inherits the properties or methods of one class into the other class. The concept behind inheritance, take the base class that contains methods and properties and inherits it into the derived class, that now contains all the members and properties of the base class as well as its own newly added members or properties. The following are the advantages of Inheritance:-
- Reuse the existence code
- Save time, we need not do the code verification and testing.
- Here, we can add and modify the methods of the existing class.
- Help in modularization of code.
Types of Inheritance:
The following are the types of inheritance exist in C# programming.
Single Inheritance:
Here we have single base class that is inherited by the derived class. Here the derived class has all the features of the base class and can add new features or modify existing features. The inheritance also depends on the access specifier that is used at the time of base class inheritance.
Multi-level Inheritance:
In the multi-level inheritance, here we having a chain of inheritance i.e. the base class A is inherited by derived class B, and it is further inherited by another derived class C. So, the feature we have in base class A, is also there in Derived class B, and all the combination features of class A and class B are there in derived class C.
Multiple Inheritance:
In C#, we don't have multiple inheritance where one class can inherit two base classes. So, the multiple inheritance can be done by using the concept of interfaces. Here one class can inherit one class and can implements more than one interface.
Hybrid Inheritance:
The hybrid inheritance can also be done through with the help of interface. Here the derived class cans implements more than two interfaces and only one class.
Example: Demonstrate Inheritance

DerivedClass7.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace cSHARPEXAMPLES
{
public class parentClass7
{
String str = "Parent:::" ;
public parentClass7() {
str = "Parent Constructor" ;
}
public String display() {
return str;
}
public String Show() {
return "Base Show" ;
}
}
public class DerivedClass7 : parentClass7
{
String testStr = "Derived::" ;
public DerivedClass7() {
testStr = "Derived Constructor" ;
}
public String display1() {
return testStr;
}
public String Show1()
{
return base.Show();
}
}
}
Form7.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace cSHARPEXAMPLES
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
private void button1_Click( object sender, EventArgs e)
{
DerivedClass7 obj = new DerivedClass7 ();
MessageBox.Show( "Call to Parent Method :: " + obj.display());
MessageBox.Show( "Call to Derived Method :: " + obj.display1());
}
private void button2_Click( object sender, EventArgs e)
{
DerivedClass7 obj = new DerivedClass7 ();
MessageBox.Show( "Call in Derived method that further call Base method ===>" + obj.Show1());
}
}
}
OutPut:
When User Clicks On :Simple Inheritance Button


When User Clicks On :Calling Base Method in Derived Method Button

|