Reading — step 1 of 5
Learn
~1 min readClasses and Properties
VB.Net supports single inheritance + multiple interface implementation.
Class Animal
Public Property Name As String
Public Sub New(name As String)
Me.Name = name
End Sub
Public Overridable Function Sound() As String
Return "..."
End Function
End Class
Class Dog
Inherits Animal
Public Sub New(name As String)
MyBase.New(name)
End Sub
Public Overrides Function Sound() As String
Return "woof"
End Function
End Class
Inherits BaseClass— single baseOverridable(base) +Overrides(derived) — virtual methodsMyBase— refers to the parent (likesuperin Java)MustInherit Class— abstract; can't instantiateMustOverride Sub/Function— abstract methodNotInheritable Class— sealed (no further inheritance)NotOverridable— locks an override down
Polymorphism:
Dim a As Animal = New Dog("Rex")
Console.WriteLine(a.Sound()) ' "woof" — virtual dispatch
Interfaces:
Interface IGreetable
Function Hello() As String
End Interface
Class English
Implements IGreetable
Public Function Hello() As String Implements IGreetable.Hello
Return "hi"
End Function
End Class
Note Implements IGreetable.Hello — VB requires you to name which interface method this implements (allows different naming).
Multiple interfaces — comma-separated after Implements.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…