Can Multiple Inheritance implemented in C# ?
Can Multiple Inheritance implemented in C# ?
No, C# does not support multiple inheritance of classes. In C#, a class can only inherit from a single base class. However, C# provides an alternative mechanism called interfaces, which allows a class to implement multiple interfaces.
Interfaces in C# define a contract that a class must follow. They specify a set of methods, properties, events, and indexers that a class must implement. A class can implement multiple interfaces, allowing it to inherit behavior from multiple sources.
Here's an example:
public interface IFly
{
void Fly();
}
public interface ISwim
{
void Swim();
}
public class Duck : IFly, ISwim
{
public void Fly()
{
Console.WriteLine("The duck is flying.");
}
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.