0

So, we have a public interface, something like:

Public Interface PublicInterface
    Sub DoStuff()
End Interface

And a class with internal visibility implementing the public interface:

Friend Class InternalClass
    Implements PublicInterface

    Friend Sub DoStuff() Implements PublicInterface.DoStuff
        'Why is this allowed? This is accessible to "public" clients!
    End Sub

End Class

I guess I'm confused because the whole point of an interface is to be able to interface with it... something like this:

Public Class AClassInAssembly
    Public Shared Function GetPublicInterface() As PublicInterface
        Return New InternalClass()
    End Function
End Sub

So you can call it like this:

Public Class AClassOutsideAssembly
    Sub Main()
        AClassInAssembly.GetPublicInterface().DoStuff()
    End Sub
End Class

TLDR version: why can you change the access modifier on an interface's method? I don't want anyone to think that they're all of a sudden restricting access to that method that IS essentially public regardless of whether or not the class is.

Can you change a setting to not allow this?

Joseph Nields
  • 5,527
  • 2
  • 32
  • 48
  • http://stackoverflow.com/questions/6040785/why-do-interface-members-have-no-access-modifier – jwatts1980 Apr 08 '15 at 14:22
  • @jwatts1980 that's not relevant here. I'm looking for a reason why you can 'change' the access modifier on a method that is an interface implementation (which doesn't really change it). Despite `DoStuff` being declared `Friend` this method is still accessible outside the assembly because clients can interface with `PublicInterface`. – Joseph Nields Apr 08 '15 at 14:23
  • Maybe I'm not following you. Did you read the marked answer? The point of an interface is to define a contract; a contract that another class must match. Changing the modifier on the class does change he access for that specific implementation of the interface. – jwatts1980 Apr 08 '15 at 14:26
  • @jwatts1980 exactly. So why can we declare `Friend Sub DoStuff() Implements PublicInterface.DoStuff` when `PublicInterface` is public? – Joseph Nields Apr 08 '15 at 14:27
  • 1
    @jwatts1980 I found an answer. Linked it if you're curious. – Joseph Nields Apr 08 '15 at 14:33
  • I guess the moral is: use care when implementing public interfaces. If you have functionality that needs to be scoped in a particular way, maybe don't put it in an interface method. – jwatts1980 Apr 08 '15 at 15:15

0 Answers0