Before we get started on the topic of Inheritance, please check out Understanding Objects.  Now, onward…

According to dictionary.com, one definition of inheritance is:

2. the genetic characters transmitted from parent to offspring, taken collectively.

This definition also fits with Object Oriented Programming.  Remember, that our Objects mimic those found in real-life and just like in real-life, Objects can have Parent Objects or Children Objects.  Inheritance is the process of obtaining the properties and methods from another Object.  The Child Object inherits these properties and methods from the Parent Object.

Using Inheritance, we can extend the functionality of a Class Object without having to modify that Object.  This helps you make your own code more reusable as well as gives you the ability to improve other people’s Class Objects.

Access Modifiers

I had talked breifly about Access Modifiers in Understanding Objects.  When building Parent and Child Class Objects, you will most likely be using all three: Private, Public and Protected.

A Child Class that inherits from a Parent does not gain access to all of the Parent’s properties and methods.  The Parent uses Encapsulation and Data Hiding to protect it’s Private properties and methods.  The Child Class can only access those properties and methods that are Protected or make up the Public Interface for the Parent Class.

The Protected access modifier acts similar to Private, keeping the properties and methods hidden from outside use, but it will allow Child Class Objects access to them.

Below is an example of a Parent Class:

Public Class MyParent

    Private _hiddenVariable As String
    Private _errors As String

    Public Sub New()

        _hiddenVariable = “”
        _errors = “”

    End Sub

    Protected Sub AddError(error As String)

        _errors += error + VbCrLf

    End Sub

    Public Function GetErrors()

        Return _errors

    End Function

End Class

Below is an example of a Child Class:

Public Class MyChild
    Inherits MyParent

    Public Sub New()

        MyBase.New()

    End Sub

    Public Function MyNewFunction()

        Return "It Worked!"

    End Function

End Class

In the above examples, the Parent Class has a couple Private string variables.  The subroutine, AddError, would normally be a Private function, but since we want the Child Class to have access to this function, we must change it to Protected.

There are two methods on MyParent’s Public Interface: New() and GetErrors().

The MyChild Class uses Inheritance to extend MyParent’s functionality.  In VB, the Inherits keyword is used to tell which class is the Parent.  Within MyChild, all of the functions on the Public Interface for MyParent as well as the Protected function, AddError, can be accessed.

There are three methods on MyChild’s Public Interface: New(), GetErrors(), and MyNewFunction().

Overriding Methods

When developing a Child Class, you may find that you need to change the way that one of the Parent Class’ methods work.  To do this, you must override the method in the Child Class.  When an instance of the Child Class is used, the overridden function would be used within the Child Class instead of the Parent Class.

Some languages allow you to override the function by simply declaring the same function in the Child as in the Parent.  In VB, you would use the Overrides keyword to declare you are overriding a function.

Protected Overrides Sub AddError(error As String)

    _errors += "MyChild:" + error + VbCrLf

End Sub

This subroutine would be placed in the MyChild Class and would override the AddError subroutine found in the MyParent Class.  Errors that occur in the Child Class would show “MyChild:” in front of them.

Also, the Child method that overrides the Parent, can also call the Parent’s method.  This allows you the power of performing some small change without having to re-implement the entire method in the Child Class.  In VB, you would use the MyBase keyword to signal you are looking for the Parent Class version of the method.

MyBase.AddError("my error")

Abstraction

Abstraction is the process of declaring a Class, property or method without giving the details on the implementation of that element.  A Parent Class can use Abstraction to declare that a function must be present in any Child Class of that Parent.  The Child Class can then either define what the method does or declare the method as Abstract also.

If a Class is Abstract, then an instance of that Class can not be created.  You must instead, access the Class through one of it’s children. 

In VB, you can force a Class to be Abstract by using the MustInherit keyword.

Public MustInherit Class MyParent

Below is an example of the MyParent Class if we wanted to make the AddError function Abstract. 

Public Class MyParent

    Private _hiddenVariable As String
    Private _errors As String

    Public Sub New()

        _hiddenVariable = “”
        _errors = “”

    End Sub

    Protected MustOverride Sub AddError(error As String)

    Public Function GetErrors()

        Return _errors

    End Function

End Class

Advantages of Inheritance, Abstraction, and Overriding Methods

  • Inheritance helps you extend the functionality of your own classes or other people’s classes.
  • Abstraction can be useful if you need multiple Classes that have similar properties and methods.  You can create an Abstract Parent Class with the common properties and methods and have the Children Classes hold the unique properties and methods.
  • Overriding Methods allows you to change the functionality of a Parent Class without modifying the Parent’s code.