In the following example i would like to hide the .sort() method to the client, how could i achieve that ?
Namespace test
Class Figure
Implements IComparable(Of Figure)
Public Property Area As Double
Public Function CompareTo(ByVal other As Figure) As Integer Implements System.IComparable(Of Figure).CompareTo
CompareTo = Me.Area.CompareTo(other.Area)
End Function
End Class
Class Figures
Inherits System.Collections.Generic.List(Of Figure)
Public Shadows Sub Add(ByVal nieuweFiguur As Figure)
MyBase.Add(nieuweFiguur)
Me.Sort()
End Sub
End Class
Class Client
Public Shared Sub Main()
Dim figures As New Figures
figures.Add(New Figure With {.Area = 12})
figures.Add(New Figure With {.Area = 16})
'***********************************************************
figures.Sort() 'i want to hide the sort method to the client
'***********************************************************
End Sub
End Class
End Namespace
Quite simply, if you don't want a caller to be able to use an instance of your class as if it were an instance of the base class, you shouldn't have that inheritance relationship to start with - it breaks the Liskov Substitution Principle.
I strongly suspect that Figures
should use composition instead of inheritance - so it would have a private field of List(Of Figure)
instead of deriving from it, and you'd expose whichever operations you want to, and only those operations. Most operations could probably just delegate to the list.