SP16

What is the use of an Abstract Class implementing an Interface Can someone throw some light on this topic please.

We cannot instantiate the abstract class. We can also not decorate the methods in this class using Shared keyword.

I was hoping to be able to access the methods by just using the class without having to instantiate it at the same time I wanted this abstract class to provide a standard(known) interface(implementing the interface)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Interface Definition
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Option Strict On
Option Explicit On

Public Interface ITestInterface

Sub DoSomething( _
ByVal takeSomething As String _
)

End Interface
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Abstract Class Definition
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Option Explicit On
Option Strict On

Public MustInherit Class TestAbstractClass
Implements ITestInterface

Public Shared Sub DoSomething( _
ByVal takeSomething As String _
) Implements ITestInterface.DoSomething

MessageBox.Show(takeSomething)
End Sub

End Class
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------




Re: Visual Basic Language Abstract Class Implementing an Interface

Alex Moura

Not sure of why you're trying to do this. Typically if you want to have a common functionality to perform but don't want to be restricted to an inheritance structure you use interfaces.

If you want to have a common base class that in practice becomes a common interface for multiple derived classes, but by itself doesn't do anything, that's where you use an abstract class, typically with some mustoverride method(s) that behave differently on each derived class.

Note that if you really want to call a shared method through an interface you can, by using an instance method to implement the interface that then calls the shared method:

Public MustInherit Class TestAbstractClass
Implements ITestInterface

Public Shared Sub DoSomething( _
ByVal takeSomething As String _
)
MessageBox.Show(takeSomething)
End Sub

Public Sub DoSomethingInstance( _

ByVal takeSomething as String _

) Implements ITestInterface.DoSomething
DoSomething(takeSomething)
End Sub


End Class






Re: Visual Basic Language Abstract Class Implementing an Interface

John Oliver (UK)MSP, VSIP

SP16 wrote:
What is the use of an Abstract Class implementing an Interface Can someone throw some light on this topic please.

We cannot instantiate the abstract class. We can also not decorate the methods in this class using Shared keyword.

I was hoping to be able to access the methods by just using the class without having to instantiate it

at the same time I wanted this abstract class to provide a standard(known) interface(implementing the interface)

Hi,

  • Create a DLL VB.Net CLASS library file.
  • Add a reference to that file in a new project.
  • Add an IMPORTS statement at the top of your 2nd project to access the PUBLIC methods within your DLL CLASS library project.

If the DLL project is Project1 and the CLASS is called myAbstractClass

then in Project2 add this line at the top of a FORM in Project2.

IMPORTS Project1.myAbstractClass

I am not sure about INTERFACES though.

Regards,

John