Multiple Inheritance Program in C Sharp using interface

Multiple inheritance in C# using interface with example programs – Multiple inheritance in C# program can be implemented using interfaces not classes.

Note that C# does not support multiple inheritance using classes. But, we can achieve multiple inheritance using interfaces in C# programming .

In other words, in C#, a class can extend only one class but can implement multiple interfaces. Recommended to read the following topics before moving further.

Multiple inheritance program in C# using interface

Below is very simple C# program. In this C# program, Bird class will extend one class (Color) and achieve multiple inheritance behavior by implementing two interfaces i.e. IFlyable and IEatable .

 /*----------------------------------------
 * Multiple Inheritance in C# using interface example program
 */
 
//Color class will be extended by derived class
//Bird
class Color {
	public void red() {
 
		Console.WriteLine("Green...");
	}
}
 
/*----------------------------------------
 * Two Inheritances that Bird class will implement 
 */
 
interface IFlyable {
	void fly();
}
 
interface IEatable {
	void eat();
}
 
// Bird class will implement interfaces and extend Color class
class Bird : Color , IFlyable, IEatable {
	// Implement method of interfaces
	public void fly() {
		Console.WriteLine("Bird flying");
	}
 
	public void eat() {
		Console.WriteLine("Bird eats");
	}
	// It can have more own methods.
}
 
/*----------------------------------------
 * Test program  - multiple inheritance using interface
 */
public class MultipleInheritance {
 
	public static void Main(String[] args) {
 
		Bird b = new Bird();
		// Call methods implemented by Bird class
		// from two interfaces
		b.eat();
		b.fly();
		// call method inherited from Color class
		b.red();
 
	}
 
}
 

Output:
Bird eats
Bird flying
Green…

Related Posts