Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# and Multiple Inheritance
C# does not support multiple inheritance of classes, meaning a class cannot inherit from more than one base class directly. However, C# provides interfaces to achieve similar functionality to multiple inheritance by allowing a class to implement multiple interfaces.
Multiple inheritance allows a class to inherit features from multiple parent classes. While this can be powerful, it can also lead to complexity and ambiguity, which is why C# chose to support only single inheritance of classes.
Why Multiple Inheritance is Not Supported
C# avoids multiple class inheritance to prevent the diamond problem, where ambiguity arises when two parent classes have methods with the same name. Instead, C# uses interfaces to provide a cleaner approach to achieving multiple inheritance-like behavior.
Syntax
Following is the syntax for implementing multiple interfaces in C# −
class ClassName : BaseClass, Interface1, Interface2 {
// Implement all interface methods
}
Following is the syntax for defining an interface −
public interface InterfaceName {
returnType MethodName(parameters);
}
Using Interfaces for Multiple Inheritance
The following example shows how to achieve multiple inheritance using interfaces. The Rectangle class inherits from the Shape base class and implements the PaintCost interface −
using System;
namespace MyInheritance {
class Shape {
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
protected int width;
protected int height;
}
public interface PaintCost {
int getCost(int area);
}
class Rectangle : Shape, PaintCost {
public int getArea() {
return (width * height);
}
public int getCost(int area) {
return area * 80;
}
}
class RectangleDemo {
static void Main(string[] args) {
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(8);
Rect.setHeight(10);
area = Rect.getArea();
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
}
}
}
The output of the above code is −
Total area: 80 Total paint cost: $6400
Multiple Interface Implementation
A class can implement multiple interfaces simultaneously. Here's an example with two interfaces −
using System;
public interface IDrawable {
void Draw();
}
public interface IResizable {
void Resize(double factor);
}
class Shape {
protected double width, height;
public Shape(double w, double h) {
width = w;
height = h;
}
}
class Rectangle : Shape, IDrawable, IResizable {
public Rectangle(double w, double h) : base(w, h) { }
public void Draw() {
Console.WriteLine($"Drawing rectangle: {width} x {height}");
}
public void Resize(double factor) {
width *= factor;
height *= factor;
Console.WriteLine($"Resized to: {width} x {height}");
}
public double GetArea() {
return width * height;
}
}
class Program {
static void Main() {
Rectangle rect = new Rectangle(5, 3);
rect.Draw();
Console.WriteLine($"Area: {rect.GetArea()}");
rect.Resize(1.5);
Console.WriteLine($"New Area: {rect.GetArea()}");
}
}
The output of the above code is −
Drawing rectangle: 5 x 3 Area: 15 Resized to: 7.5 x 4.5 New Area: 33.75
Class Inheritance vs Interface Implementation
| Class Inheritance | Interface Implementation |
|---|---|
| Single inheritance only | Multiple interfaces allowed |
| Inherits implementation | Must provide implementation |
| Can have constructors | Cannot have constructors |
| Can have fields and properties | Cannot have fields (before C# 8.0) |
Conclusion
C# achieves multiple inheritance through interfaces rather than multiple class inheritance. This approach avoids complexity and ambiguity while providing flexibility to implement multiple contracts. A class can inherit from one base class and implement multiple interfaces simultaneously.
