stu nicholls dot com | menu - Professional dropdown #3

click here
Inheritence

Syntax Inherits classname

Here the class name is the name of the class which is being inherited

Note:In the example shown we have to create the instance of the main form because the Animal and Dog classes are different than the Form class. So, to use the TextBox or Button controls of the main form we have to create the instance of it.

Public Class NewExampleInh
Inherits System.Windows.Forms.Form
Dim dolly As child
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dolly = New child(Me)
dolly.dancing()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
dolly.crying()
Catch
MsgBox("Dolly is not born")
End Try
End Sub
End Class
Public Class Human
Public mainform As New NewExampleInh()
Public Sub New(ByRef form1 As NewExampleInh)
mainform = form1
End Sub
Public Sub dancing()
mainform.TextBox1.Text = "Dancing"
End Sub
End Class
Public Class child
Inherits Human
Public Sub New(ByRef form1 As NewExampleInh)
MyBase.New(form1)
End Sub
Public Sub crying()
mainform.TextBox1.Text = "Crying"
End Sub
End Class

  1. Using public Inheritance
  2. Using Protected Inheritance
  3. Using Private
  4. Using Friend
Using Private members:
Public Class NewExamplePrivate
Inherits System.Windows.Forms.Form
Dim dolly As child
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dolly = New child(Me)
dolly.dancing()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
dolly.crying()
Catch
MsgBox("Dolly is not born")
End Try
End Sub
End Class
Public Class Human
Private mainform As New NewExamplePrivate()
Public Sub New(ByRef form1 As NewExamplePrivate)
mainform = form1
End Sub
Public Sub dancing() mainform.TextBox1.Text = "Dancing" End Sub
Public Sub UsePrivateMembers()
mainform.TextBox1.Text = "Crying"v End Sub
End Class
Public Class child
Inherits Human
Public Sub New(ByRef form1 As NewExamplePrivate)
MyBase.New(form1)
End Sub
Public Sub crying()
UsePrivateMembers()
End Sub
End Class

Inheritance Modifiers

By default, all classes can serve as base classes in VB.NET. However, you can use two class level modifiers, called inheritance modifiers, to modify that behavior
NotInheritable-Prevents a class being used as a base class
Example: Public NotInheritable Class Human
MustInherit-Indicates that the class is intended for use as a base class only. The MustInherit keyword is used to declare a class that can not be instantiated and can be used only as base class, also called as an abstract class.
Example: Public MustInherit Class Human. So you can not create an object of Human class.

Using MustInherit
Public Class NewExampleInh
Inherits System.Windows.Forms.Form
Dim dolly As child
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dolly = New child(Me)
dolly.dancing()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
dolly.crying()
Catch
MsgBox("Dolly is not born")
End Try
End Sub
End Class


Public MustInherit Class Human
Public mainform As New NewExampleInh()
Public Sub New(ByRef form1 As NewExampleInh)
mainform = form1
End Sub
Public Sub dancing()
mainform.TextBox1.Text = "Dancing"
End Sub
End Class
Public Class child
Inherits Human
Public Sub New(ByRef form1 As NewExampleInh)
MyBase.New(form1)
End Sub
Public Sub crying()
mainform.TextBox1.Text = "Crying"
End Sub
End Class

Types of Properties<< Previous   Next>>Overloading


comments powered by Disqus
click here
click here
click here
Footer1