C# | Can an inheritence be multiple and multilevel at the same time?

4085 views c#
7

So i have a class, called "Audi". Now this class inherits from base abstract class called "Car", now "Car" inherits from abstract class "Vechile". But also, I want class "Audi" to inherit from an interface called "IUnlockCar". My question is as the title mentions. And if it can't be mulitlevel and multiple at the same time, then what type could I call this inheritence?

interface IUnlockCar{}
public abstract class Vechile{}
public abstract class Car : Vechile{}
public class Audi : Car, IUnlockCar{}

answered question

sure it works, why didn't you just check with a compiler?!

Classes do not inherit from interfaces, they implement them. A class in c# can only have one base class it inherits, but can (and often do) implement multiple interfaces, either directly or by inheritance.

1 Answer

7

In C# you can extend only one class and implement multiple interfaces. The purpose of it is to prevent the Diamond problem

posted this

Have an answer?

JD

Please login first before posting an answer.