I'm facing a problem regarding multiple inheritance in VB.net:
As far as I know VB.net does not support multiple inheritance in general but you can reach a kind of multiple inheritance by working with interfaces (using “Implements” instead of “Inherits”):
Public Class ClassName
Implements BaseInterface1, BaseInterface2
End Class
That works fine for classes but I’d like to have an interface inheriting some base interfaces. Something like that:
Public Interface InterfaceName
Implements BaseInterface1, BaseInterface2
End Interface
But the “Implements” keyword is not allowed for interfaces (what makes sense, of course). I tried to use a kind of abstract class which I know from Java:
Public MustInherit Class InterfaceName
Implements BaseInterface1, BaseInterface2
End Class
But now I need to implement the defined methods from BaseInterface1 and BaseInterface2 within the InterfaceName class. But as InterfaceName should be an interface, too, I don’t want to have to implement these methods within that class.
In C# you can do that quite easy:
public interface InterfaceName: BaseInterface1, BaseInterface2 {}
Do you know if I can do something similar in VB.net?
An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class. Multiple inheritance by interface occurs if a class implements multiple interfaces or also if an interface itself extends multiple interfaces.
Unlike languages that allow multiple inheritance, Visual Basic allows only single inheritance in classes; that is, derived classes can have only one base class. Although multiple inheritance is not allowed in classes, classes can implement multiple interfaces, which can effectively accomplish the same ends.
An interface cannot inherit from an interface nested within it.
Inheritance can have multiple levels. That is, a base class can have derived classes, each of which can have derived classes, and so on. These classes constitute what is known as an inheritance hierarchy (or inheritance tree).
Similar to Java, in VB.NET interfaces "extend" other interfaces. That means they "inherit" their functionality. They do not implement it.
Public Interface InterfaceName
Inherits BaseInterface1, BaseInterface2
End Interface
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With