C# Inheritance


  • Inheritance is a most important feature of the Object Oriented Programming Language.
  • Inheritance supports the concept of reusability and extensibility.
  • Extensibility in Object Oriented Programming is achieved by Sub-Classing. The process of sub-classing to a class extends its functionality that is known as Inheritance.
  • The original or existing class is known as base, parent or super class and the class which inherits the functionality of base class is known as the derived, child or sub class.

Like Java C# doesn’t support Multiple Inheritance. The concept of multiple inheritance can be used in the form of Interfaces which we will discuss in further section.


Defining Base class and Derived class


Base class is a original class so it is simply defined as a normal class But derived class inherited by the Base class which is defined by using ‘:’. The syntax to define base class and derived class is given below:


class Baseclass-name
{
Variables declaration;
Methods declaration;
}
class derivedclass-name: Baseclass-name
{
Variables declaration;
Methods declaration;
}

Here is a simple example of Inheritance in C#


Example

using System;
namespace Inheritance
{
//Base-Class definition
class ClassA
{
protected int a = 10, b = 20;
public void display()
{
Console.WriteLine("a={0}\nb={1}", a, b);
}
}
//Derived-Class definition
class ClassB : ClassA
{
public int c = 20, d = 30;
public void display1()
{
Console.WriteLine("c={0}\nd={1}", c, d);
}
public void addelements()
{
int z;
z = a + b + c + d;//You can use protected variables of base class in derived class
Console.WriteLine("Result of Addition:{0}", z);
}
}
class mainfunction
{
static void Main()
{

ClassB b = new ClassB();
b.display();//calling the method of base class by object of derive class
b.display1();//calling method of derived class
b.addelements();//calling methods of derived class
Console.ReadLine();
}
}
}

The output of the above program will be as follows:


Output

a=10
b=20
c=20
d=30
Result of Addition: 80

C# Interfaces


  • Multiple Inheritance is not possible in C# that is why interface is used to implement multiple inheritance in C#.
  • Interfaces are special kind of type in C#. An Interface contains signature of the methods, properties and events.
  • An Interface is a type whose all members are public and abstract by default.
  • An Interface contains only declaration of members; these members are further defined in the base class.
  • A class can implement more than one interface.

Declaration of C# Interface


  • Interfaces are declared using the interface keyword.
  • It is similar to class declaration.
  • An simple example to declare an interface is as follows:
    Interface IMyName
    {
    void displaymyname();//Only declaration of method
    ………………
    ……………
    }

Multiple Inheritance with Interfaces


In Multiple Inheritance one sub-class is derived from the more than one base classes. Here is an example of interface which implements the Multiple Inheritance:


Example

using System;
namespace InterfaceExample
{
public interface interface1
{
void printacity();//Method declaration in Interface1
}
public interface interface2
{
void printastate();//Method declaration in Interface2
}
//New class implements the two interfaces interface1 and interface2
public class newclass: interface1,interface2//derved from more than one interface i.e multiple inheritance
{
public void printacity()//define the method declare in interface1
{
Console.WriteLine("City is Jaipur");
}
public void printastate()//define the method declare in interface2
{
Console.WriteLine("State is Rajasthan");
}
public void printacountry()//own method of newclass
{
Console.WriteLine("Country is India");
}
}
class mainfunction
{
static void Main()
{
newclass a = new newclass();
a.printacity();
a.printastate();
a.printacountry();
Console.ReadLine();
}
}
}

The output of the above program will be as follows:


Output

City is Jaipur
State is Rajasthan
Country is India