C# Multiple Inheritance. How to implement multiple inheritance in C#

Facebook
Twitter
LinkedIn

Never miss a post!

Sign up for our newsletter and get FREE Development Trends delivered directly to your inbox.

You can unsubscribe any time. Terms & Conditions.
Categories

In Object Oriented Programming language, a class or an object can inherit features and characteristics of one or more parent object or class. It is used when a sub class needs to access the some or all the properties of a parent class. It is also useful when the child class needs to combine multiple contracts of a base class. C++ allows the provision of having multiple inheritance in sub classes.

Why is multiple inheritance not allowed in C#?

In C#, a child class can only have a one-parent class as the designers of this language wanted to keep the class hierarchy simple by restricting the implementation of multiple inheritance.

Work around to implement multiple inheritance in C#

There are several ways to achieve the functionality of multiple inheritance in C# language. The most famous and easy method is to use the combination of interfaces.

using System; 
using System.Collections; 

// Parent or Base Class A 
public class ProgrammingLanguage { 

	// Providing the implementation of programming languages() method 
	public void GetProgrammingLanguages() 
	{ 
		// create an arraylist 
		ArrayList list = new ArrayList(); 

		// add items in list 
		list.Add("C#"); 
		listAdd("C++"); 
		list.Add("C"); 
		list.Add("Java"); 
    list.Add("Python");

		// print item in the language list 
		foreach(var item in list ) 
		{ 
          Console.WriteLine("\n");
		      Console.WriteLine(item); 
		} 
	} 
} 

// Parent or Base Class B 
public class Course { 

	public void GetCourses() 
	{ 

		// create a list for courses 
		ArrayList list = new ArrayList(); 

		// add items in course list
		list.Add("Database Architecture"); 
		list.Add("Object Oriented Programming"); 
		list.Add("Management Information System"); 
		list.Add("Automata Theory"); 
    list.Add("Introduction to Programming");

		// print items in course list  
		foreach(var item in list) 
		{ 
		      Console.WriteLine("\n");
		      Console.WriteLine(item); 
		} 
	} 
} 

// Child or Sub class  to inherit one or more classes
public class Enrollment : ProgrammingLanguage, Course { 
} 

// Adding Main Class
public class Program { 

	// Main method 
	public static public void Main() 
	{ 

		// Creating an object of Enrollment class 
		Enrollment objEnrollment = new Enrollment (); 
		objEnrollment.GetProgrammingLanguages(); 
		objEnrollment.GetCourses(); 
	} 
}

The above program will throw a runtime exception of ‘Classes cannot have multiple base classes’. The following program is used to implement multiple class inheritance using interfaces in C#

using System; 
using System.Collections; 

// Create Interface for the class Programming Language
public interface IProgrammingLanguage { 
	public void GetProgrammingLanguages (); 
} 

// Parent Class A ProgrammingLanguage 
public class ProgrammingLanguage : IProgrammingLanguage { 

	// Providing the implementation of programming languages() method 
	public void GetProgrammingLanguages() 
	{ 
		// create an arraylist 
		ArrayList list = new ArrayList(); 

		// add items in list 
		list.Add("C#"); 
		listAdd("C++"); 
		list.Add("C"); 
		list.Add("Java"); 
    list.Add("Python");

		// print item in the language list 
		foreach(var item in list ) 
		{ 
		      Console.WriteLine("\n");
		      Console.WriteLine(item); 
		} 
	} 
} 
// Create Interface for the class Courses
public interface ICourses{ 
	public void GetCourses (); 
} 

// Parent Class B Course 
public class Course { 
	public void GetCourses() 
	{ 
		// create a list for courses 
		ArrayList list = new ArrayList(); 
		// add items in course list
		list.Add("Database Architecture"); 
		list.Add("Object Oriented Programming"); 
		list.Add("Management Information System"); 
		list.Add("Automata Theory"); 
    list.Add("Introduction to Programming");

		// print items in the course list  
		foreach(var item in list) 
		{ 
		      Console.WriteLine("\n");
		      Console.WriteLine(item); 
		} 
	} 
} 

// Child or Sub class  to inherit both interfaces ProgrammingLanguage and Course
public class Enrollment : IProgrammingLanguage, ICourse { 

  // Creating objects of ProgrammingLanguage and Course class 
	IProgrammingLanguage objA = new ProgrammingLanguage (); 
	ICourse objB = new Course (); 
	public void GetProgrammingLanguages() 
	{ 
		objA.GetProgrammingLanguages (); 
	} 

	public void GetCourses() 
	{ 
		objB.GetCourses(); 
	} 
} 

// Adding Main Class to print the result
public class Program { 

	// main method 
	public static public void Main() 
	{ 

		// Creating an object of Enrollment class 
		Enrollment objEnrollment = new Enrollment (); 

    // access the methods 
    Console.WriteLine("\n The following Programming Languages are available: \n ");
		objEnrollment.GetProgrammingLanguages(); 

    Console.WriteLine("\n \n The following Courses are available: \n ");
		objEnrollment.GetCourses(); 
	} 
}

Output of the above code


The Following Programming Languages are available:
C#
C++
C
Java
Python

The following Courses are available:
Database Architecture 
Object Oriented Programming 
Management Information System
Automata Theory
Introduction to Programming

Conclusion

Inheritance is one of the main pillar of Object Oriented Programming as it allows the class to be defined in a hierarchical manner. It increase the reusability of the code, thus saving time and effort of a developer by extending the software code. Inheritance is ‘is-a’ relationship between children and parent. In C#, single inheritance is allowed only but multiple inheritance can be achieved by using one or multiple interfaces simultaneously. Another advantage of using interfaces is hiding the implementation of a class from outside world.

Facebook
Twitter
LinkedIn

Our website uses cookies that help it to function, allow us to analyze how you interact with it, and help us to improve its performance. By using our website you agree by our Terms and Conditions and Privacy Policy.