How to achieve multiple inheritance in C#
Solutions in C#
public interface IFlyable { void Fly(); } public interface ISwimmable { void Swim(); } public class Duck : IFlyable, ISwimmable { public void Fly() { Console.WriteLine("Duck is flying."); } public void Swim() { Console.WriteLine("Duck is swimming."); } }public class Engine { public void Start() { Console.WriteLine("Engine started."); } } public class Wheels { public void Roll() { Console.WriteLine("Wheels are rolling."); } } public class Car { private Engine _engine = new Engine(); private Wheels _wheels = new Wheels(); public void Drive() { _engine.Start(); _wheels.Roll(); Console.WriteLine("Car is driving."); } }public class Animal { public void Eat() { Console.WriteLine("Animal is eating."); } } public class Bird { public void Fly() { Console.WriteLine("Bird is flying."); } } public class FlyingAnimal { private Animal _animal = new Animal(); private Bird _bird = new Bird(); public void Eat() { _animal.Eat(); } public void Fly() { _bird.Fly(); } }
Last updated