Inheritance & Interfaces¶
CrossBasic supports single inheritance and interface implementation, allowing you to build rich type hierarchies.
Inheritance¶
Use the Extends keyword to derive a subclass:
Class Animal
Sub Speak()
Print("…")
End Sub
End Class
Class Dog Extends Animal
Overrides Sub Speak()
Print("Woof!")
End Sub
End Class
Var d As New Dog
d.Speak() // prints "Woof!"
- Base methods can be overridden with
Overrides. - Call
MyBase.MethodName(...)to invoke the parent’s implementation.
Interfaces¶
Define an interface with Interface, then implement it:
Interface IShape
Sub Draw()
End Interface
Class Circle Implements IShape
Dim Radius As Double
Sub Draw() Implements IShape.Draw
// draw a circle of given Radius
End Sub
End Class
Var c As New Circle
c.Draw()
- Interfaces declare method signatures.
- Classes implement with
Implements InterfaceName. - Each implementing method must include
Implements InterfaceName.MethodName.