Inheritence


  • It is an object-oriented programming term. The ability of a subclass to take on the characteristics of the class it's based on. The process by which the class inherits the characteristics of a base class is called as subclassing. If the characteristics of the parent class change, the subclass on which it is based inherits those characteristics. For example, if you add a new property, IsBold, to an editing control, any subclasses based on your control will also have an IsBold property

  • Inheritance provides for

  • Hiding Unnecessary Complexity
  • Leveraging the Power of Existing Classes
  • Subclassing allows you to reuse code.
  • Streamlining Code Maintenance
  • Inheritance makes maintaining your code easy.
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
    • In this the public members of a base class becomes public members of derived class
  2. Using Protected Inheritance
    • When a member is declared as protected in base class, then it is available throughout the class and in the derived class
    • In the previous example MainForm gets more scope than needed and it is contrary to the idea of data hiding in OOP. So MainForm is declared as protected, which restricts its scope to the current (Human) class and the classes derived from it like child
  3. Using Private
    • If you make a member private then it is available only within the class and not in the derived class.
    • In the previous example if MainForm is declared as private, which restricts its scope to the current (Human) class and it is not available to the classes derived from it like child.
    • But you can access the private members with the help of the public members of that base class. This is done using UsePrivateMembers() of Human class in child class in the next example shown.
  4. Using Friend
    • This restricts the access to the program in which they are declared and anywhere in the same assembly
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" 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

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus




Footer1