You are on page 1of 6

Inheritance and

interfaces
Inheritance IN VB.NET
keyword: inherits

Public class A Public class A1 inherits A


Public sub New() Public sub Add()
End sub End Sub
End Class End Class
ABSTRACT CLASSES

 ABSTRACT classes are similar to base classes with the difference that the
methods declared inside the abstract classes known as abstract methods
 Abstract methods do not have method body

[public] MustInherit class classname


‘body
End class
POLYMORPHISM

 METHOD OVERLOADING
 Method Overloading allows us to write different versions of a method (i.e. a
single class can contain more than one methods (Sub / Functions) of same
name but of different implementation).
 And compiler will automatically select the appropriate method based on
parameters passed
Public Overloads Function add(ByVal a As Integer, ByVal b As Integer)
Public Overloads Function add(ByVal a As Long, ByVal b As Long)
 METHOD OVERRIDING
 Method Overriding means override a base class method in the derived class
by creating a method with the same name and signatures to perform a
different task.
 The Method Overriding in visual basic can be achieved by using
Overridable & Overrides keywords along with the inheritance principle.
Method overriding example
' Base Class
Public Class Users
Public Overridable Sub GetInfo()
Console.WriteLine("Base Class")
End Sub
End Class

'Derived Class
Public Class Details
Inherits Users
Public Overrides Sub GetInfo()
Console.WriteLine("Derived Class")
End Sub
End Class
INTERFACES

 It is declared in the same way as a class


 We declare it using interface keyword
 It contains only method declarations but no body.
[accessmodifier] interface <interfacename>
‘method declarations
End interface
 Using the implements keyword to implement an interface:
Class <classname> implements <interfacename>
End class

You might also like