Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Part 5.2Abstraction and inheritance

Part 5.2Abstraction and inheritance

Two very fundamental parts of an object oriented language like VB.NET are abstraction and inheritance. They play a huge role in the whole of the .NET Framework and can be used to the full to improve program development on the whole - it is after all good practise to build reusable code, which is one of the key aims of both these technologies.

Abstraction

According to Wikipedia:

Abstraction is conceptual process by which general rules and concepts are derived from the usage and classification of specific examples

The general idea of abstraction in object oriented programming is to make development easier. It is a clever way of creating an class that does not have any purpose but to be customised. Here is an example:

Suppose there is an animal of an unknown type (it could be a cat, dog or any other animal) but then it is given some classification as a dog. It is clear that a dog differs from a cat, for instance, a cat does not bark, whereas a dog does.

In programming terms, the animal is the superclass (or root class) of dog and cat, but they do not share everything, for instance, barking. They do however, both have the ability to communicate, so the superclass animal can have this ability. However, there is no such thing as a plain animal, it has a classification for instance the type of animal.

This can be represented as a hierarchical tree:

Animal class

Both the cat and the dog are animals

Animal has some very basic commands such as walk, communicate, eat and so on, but the fact that an animal can be either a cat or a dog or any other animal, means that the animal does not have any specific way of performing these functionalities. This is solved by leaving this to the subclass. This is called abstraction.

To put this into perspective in VB.NET, a class which is the superclass will have some methods that can perform some tasks and some methods that need to be specified.

VB.NET
Public MustInherit Class Animal
	'This variable has been specified and will be passed to all subclasses
	Dim minHeight As Integer
	Dim sleeping As Boolean
	Sub New()
		minHeight = 10
	End Sub

	'This method has been left empty giving the subclass power over what it does
	Public MustOverride Sub
		Communicate()
	Public Sub Sleep()
		'Inverts the sleeping variable
		sleeping = Not sleeping
	End Sub

	Public Sub OutputHeight()
		MsgBox("My minimum height is " & minHeight)
	End Sub
End Class
		

The MustInherit is used to define that the class is abstract. Any methods, variables, structures and so on must be inherited. This also means that this class cannot be instantiated on its own such as Dim a As New Animal.

The other new keyword that appears is the MustOverride before the Sub keyword. This is used to determine that this method will not do anything on it's own but it must be given code to perform something. The subclass is the one that will define this.

Inheritance

Inheritance builds upon the fundamentals of abstraction. It is designed to transform one class into another. Inheritance however is not limited to abstract classes.

With abstract classes

In terms of abstract classes, inheritance is the way that they can be used. For the Animal class, the following sample demonstrates the inherited class.

VB.NET
Public Class Dog
	Inherits Animal

	Public Sub Bark()
		'Place code for Bark here
	End Sub

	Public Overrides Sub Communicate()
		Bark()
	End Sub
End Class
		

Most notably are the two new keywords Inherits and Overrides. The Inherits keyword is used to define which class this class will inherit properties and methods from, including variables and so on. The Overrides keyword is used to specify what a an abstract method (that's one in an abstract class with MustOverride in front of it) will do. In this case, the Communicate method is being given code that will call the method Bark. The following sample instantiates the Dog object.

VB.NET
Dim d As New Dog
d.Bark()
d.Communicate()
d.OutputHeight()
d.Sleep()
		

Both Communicate and Bark will do the same thing. To conceal the Bark method it can be declared Private using the access modifier.

With standard classes

Inheritance is not just limited to abstract classes. Standard classes are also inheritable. The following example takes the newly formed Dog class and creates a class for a labrador.

VB.NET
Public Class Labrador
Inherits Dog

Public Overrides Sub Communicate()
	MyBase.Communicate()
	MsgBox("I'm a labrador")
End Sub

End Class
		

The MyBase keyword is used to refer to the parent class of the class, that is in this case the Dog class. This means that it calls the Communicate method within the Dog class.

If this is taken a step further as shown in the following sample, the object gets deeper, but at the cost of speed (for each inherited class it will keep getting slower).

VB.NET
Public Class Jack
Inherits Labrador

Public Overrides Sub Communicate()
	MyBase.Communicate()
	MsgBox("My name is Jack")
End Sub

End Class
		

This will inherit from Labrador which in turn inherits from Dog which in turn inherits from Animal.

Classes from within the .NET Framework can also be inherited. In the following case this class is inherited from the RichTextBox control class.

VB.NET
Public Class AdvancedRTB
Inherits System.Windows.Forms.RichTextBox

Public Sub Print()
	'Place code to print rich textbox here
End Sub

End Class
		

Preventing inheritance

If a class has important methods that should remain public but should not be able to be inherited, for instance in the case of protecting a rich textbox control from others editing it, inheritance must be prevented using the NotInheritable keyword. This modifier will prevent access directly to methods and is demonstrated below with the sample shown before.

VB.NET
Public NotInheritable Class AdvancedRTB
Inherits System.Windows.Forms.RichTextBox

Public Sub Print()
	'Place code to print rich textbox here
End Sub

End Class
		

Why abstraction is useful

Abstraction is useful when there are methods that must be implemented a certain way along side methods that can be implemented another way. One example is with the Animal class which must be able to do things like Sleep, which all animals do and could be directly implemented into the class, but there are also methods such as Communicate that can be performed in different ways for each animal, so this is left to the class of animal such as Dog or Cat to implement.

Abstraction leaves scope for individualisation rather than objectification.

Feedback 👍
Comments are sent via email to me.