The interface behaves differently in Vb.Net. The following is an example code snippet where IStudent has a SayHello method, which is implemented by the Student class. The access modifier for SayHello must be Public by default. Changing the access modifier to Private does not violate the existing code, and I can still access this private method using the code below
Dim stdnt As IStudent = New Student stdnt.SayHello()
An access modifier defines the scope of members in a class; more than private members are only available from a class that exists. But here the access modifier theory, encapsulation is violated.
- Why is .net designed this way?
- Is the concept of access modifier and encapsulation really broken?
- How does the .net infrastructure handle this situation internally?
Thank you in advance
Module Module1 Sub Main() Dim stdnt As IStudent = New Student stdnt.Name = "vimal" stdnt.SayHello() End Sub End Module Public Interface IStudent Property Name As String Sub SayHello() End Interface Public Class Student Implements IStudent Private Property Name As String Implements IStudent.Name Private Sub SayHello() Implements IStudent.SayHello Console.WriteLine("Say Hello!") End Sub End Class
source share