![]() |
VOOZH | about |
Multiple inheritance means a class can inherit features from more than one base class. While this is supported in some languages, C# does not allow multiple inheritance with classes to avoid ambiguity and complexity (such as the diamond problem).
However, C# supports multiple inheritance through interfaces. A class can implement multiple interfaces and thereby acquire behavior from multiple sources.
An interface is a contract that defines:
A class that implements an interface must provide implementations for all its members. A class can implement multiple interfaces, which enables multiple inheritance-like behavior.
A class implements interfaces by listing them after the class name, separated by commas. The class must then provide implementations for all interface members.
Syntax:
class ClassName : IInterface1, IInterface2
{
// implement all interface methods here
}
This allows a single class to support multiple behaviors defined by different interfaces.
Example: In this example, a class implements two interfaces and provides implementations for both.
Area: 50 Color: Blue
Explanation:
A class implementing multiple interfaces does not need to implement all logic directly. It can delegate the implementation to helper classes while still fulfilling the interface contracts.
Languages: C C++ C# Java Courses: System Design Python DSA Java
Explanation:
Portal class while multiple behaviors are supported.