Let's say I have the following interfaces:
Public Interface IFooBase
Sub Bar()
End Interface
Public Interface IFoo
Inherits IFooBase
Sub Zim()
End Interface
I understand that a class which implements IFoo
implicitly implements IFooBase
, and that all IFooBase members can be specified via the derived interface (eg Public Sub Bar() Implements IFoo.Bar
) without issue, and no error is given if a class specifies that it implements both the base interface and the derived interface.
So what is the best practice here? My gut feeling is that the class should be implemented as follows:
Public Class FooClass
Implements IFooBase, IFoo
Public Sub Bar() Implements IFooBase.Bar
End Sub
Public Sub Zim() Implements IFoo.Zim
End Sub
End Class
My reasoning is that by explicitly specifying the base interface and declaring the class members at the level they were defined at, the code is more clear and if the derived interface were ever to be refactored, removed, or replaced, the amount of refactoring that needs to be done is strictly limited. But am I missing anything with this approach?
By implementing both IFooBase
and IFoo
, you would be implicitly stating to anyone (else) reviewing the code that IFoo
does not inherit from IFooBase
, though implementing both even though IFoo
inherits from IFooBase
is not incorrect.
However, should IFoo
be changed to also inherit from another interface, IBar
(Interfaces can have multiple inheritances, but not classes), you would be implicitly requiring FooClass
to also implement IBar
, even though FooClass
should not require the additional Implements IBar
.
Also, if you alter the base interface, you're complicating things more if your implementing classes explicitly implement it than if they implicitly implement it.
I believe that there's something to be said for only implementing the most-derived interface, and not also all the parent interfaces.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.