Abstract Class Tutorial
Abstract classes are a lot like Interfaces. They can be used to define a data type just like an Interfaces does. The one main difference is that abstract classes can have code in them. Q: Why didn't you cover abstract classes after interfaces? A: Abstract classes won't work without inheritance so it was necessary to learn about inheritance first. Q: What distinguishes an abstract class from a regular class? A: An abstract class can not be directly made an object of (with the "New" keyword) and therefore must be inherited.
It can still be used as a data type like an interface just not created. The greatest part of this is unlike an interface you can have code and therefore anything that would be common to all that inherit it could just be coded in and inherited. Anything that wouldn't be common could easily be overridden or just defined like an interface.
To illustrate this we will make an abstract class of what Square, Triangle, Rectangle, and Cube all have in common. Well they are all Shapes. Some simple commonalities are they all have a name and number of dimensions.
There's not a whole lot new in abstract classes that you wouldn't already know from the previous three tutorials so this will be quite short. Open Visual Studio .NET and load the ShapeClass project/solution that we did in the Introduction to Classes and we will continue from there.
Add a new class and call it Shape. Now it is not yet abstract for there is a one word (actually two put together) that will make it abstract. This keyword is "MustInherit" (makes sense). Conversely to make a class not inheritable then you use the "NotInheritable" keyword (also makes sense). You should end up with a class like so:
Public MustInherit Class Shape End Class
So the two function that the Shape class will be are ShapeName and NumDimensions. Now for ShapeName we can supply some default code to return the name but we will allow them to override it if they wish. As for NumDimensions we will not supply any code and just have our inheritors do that. To to declare a function that has no code in it, it is declared "MustOverride" (like inherits you can stop it from being overridden with the "NotOverridable" keyword).
Public MustInherit Class Shape
Public Overridable Function ShapeName() As String
Return MyClass.GetType.Name ' will simply return the name of the class
End Function
MustOverride Function NumDimensions() As Integer
End Class
Now time to inherit this class. A simple example will be the triangle class where we will just use the Shape's ShapeName and override the NumDimensions. The only changes/additions are:
Public Class Triangle
Inherits Shape ' now inherits from Shape
' necessary to fully inherit from Shape
Public Overrides Function NumDimensions() As Integer
Return 2
End Function
For the original Square class we will be a little different and override the ShapeName function just to show an example.
Public Class Square
Inherits Shape
Public Overrides Function ShapeName() As String
Return "A Square" ' a more descriptive name (maybe)
End Function
Public Overrides Function NumDimensions() As Integer
Return 2
End Function
Since Square2 (possibly still Square) and Rectangle implement an Interface they can still inherit Shape and the code would look similar to the two examples above.
Just like an Interface and inherited creatable Classes you can use the Abstract class as a data type and therefore you could have two Shape variables set to a Triangle and a Square. Return to the module and do just that.
Sub Main()
' previous stuff from before
Dim s, t As Shape
s = New Square()
t = New Triangle()
Console.WriteLine("t is a " & t.ShapeName & " with " & t.NumDimensions.ToString() & " dimension(s)")
Console.WriteLine("s is " & s.ShapeName & " with " & s.NumDimensions.ToString() & _
" dimension(s) and area of " & CType(s, Square).Area())
End Sub
Output:
t is a Triangle with 2 dimension(s)
s is A Square with 2 dimension(s) and area of 1
Notice that the Square's ShapeName returned "A Square" because we overwrote that function and that's what we decided to return.
Congratulations you have completed the Abstract Class Tutorial. If you would like more practice at Abstract classes then continue with the Abstract Class Example.
Author Information:
http://www.programmers-corner.com
