VB.Net - SPLessons

VB.Net Inheritance and Interfaces

Home > Lesson > Chapter 30
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

VB.Net Inheritance and Interfaces

VB.Net Inheritance and Interfaces

shape Description

Inheritance is defined as the one class acquires the properties, methods and events of the other class. Here the base class is having the properties like methods, variables, objects and the subclass will acquire all the Properties of the base class. Subclass class will Reuse the methods, variables that are defined in the base class with a base class object. The use of the Inheritance is the data in made in the hierarchical manner. The main use of the inheritance is the Re-usability.Following is an example which describes what is the concept behind the inheritance

shape Example

Following is an example which describes more about the inheritance concept. [vbnet]Imports System Imports System.Threading Module Module1 Class Person Private strName As String Public intAge As Integer Public Function getName() Return strName End Function Public Function getAge() Return intAge End Function Public Sub setName(ByVal name As String) strName = name End Sub End Class Class CricketPlayer Inherits Person Public strNationality As String Function getNationality() Return strNationality End Function End Class Sub Main() System.Console.WriteLine("Example") Thread.sleep(2000) Dim player As New CricketPlayer player.setName("kohli") player.intAge = 23 player.strNationality = "india" System.Console.WriteLine("Name :" + player.getName()) System.Console.WriteLine("Nationality :" + player.getNationality()) System.Console.WriteLine("Age :" + CType(player.getAge(), String)) End Sub End Module[/vbnet] Output When compile the program output will be as follows.

Interfaces

shape Description

The interface can be defined as a collection of abstract methods and class can be defined as box generally, inside of this box developer can write functions, methods. Class implements an interface that describes the properties, methods and events.Following is the code which describes an interface with one function, one property, and one event. [vbnet]Interface IAsset Event ComittedChange(ByVal Success As Boolean) Property Division() As String Function GetID() As Integer End Interface[/vbnet] Implements is the visual basic reserved word.Following is an example which describes how to use the kaeword implements. [vbnet]Class Class1 Implements interfaceclass.interface2 Sub Sub1(ByVal i As Integer) Implements interfaceclass.interface2.Sub1 End Sub End Class[/vbnet]

Summary

shape Points

  • All the base class properties will be hired by child class in inheritance concept.
  • Implements is the reserve keyword of visual basic.