Website Design United States, Website Design California, Website Designing United States, Website Designing California

Inheritance Tutorial

Inheritance is an important part of object oriented programming (OOP). It allows you to extend and enhance another class with out having that classes' source code or even knowing how that class works. This is great because you can create a class to your liking without changing the existing class which won't screw up any other applications that use the existing class.

Open Visual Studio .NET and create a new Console Application called InheritedCube. First we need something to inherit from. In the main menu click Project > Add Existing Item and find the Square class from the Class Tutorial. If you overwrote the Square class in the Interface Tutorial then just add a new class called Square and put the following code in it:

Private mdblLength As Double

#Region "Constructor(s)"
Public Sub New()
mdblLength = 1
End Sub

Public Sub New(ByVal SideLength As Double)
mdblLength = 1
Me.Length = SideLength
End Sub
#End Region

Public Property Length() As Double
Get
Return mdblLength
End Get
Set(ByVal Value As Double)
If Value > 0 Then
mdblLength = Value
Else
Throw New ArgumentException("Length must be greater than zero (0)")
End If
End Set
End Property

Public Function Perimeter() As Double
Return mdblLength * 4
End Function

Public Function Area() As Double
Return mdblLength ^ 2
End Function

As you may have guess the new class we will create will be a Cube. Add a new class and call it Cube. This class will inherit from the Square because a cube is basically six squares put together to form a box. Now to inherit another class you use the keyword "Inherits" on the line after the class declaration.

Public Class Cube
Inherits Square

End Class

If you return to Sub Main in the module a declare a Cube variable you will see that is has every the Square does and functions just like a Square does. This is because it inherited (just like you inherit traits from your parents) everything from the Square. Also note that it did not inherit the constructors so those will have to be re-declared. It also didn't inherit the variable for the length because it is private but there is a public property we can use to get and set the value.

The constructors for Cube will do the same as Square but since we don't have access to mdblLength inside Cube we will use the Length property.

#Region "Constructor(s)"
Public Sub New()
Length = 1
End Sub

Public Sub New(ByVal TheLength As Double)
Length = 1
Length = TheLength
End Sub
#End Region

One difference between a square and a cube is the Area (we will ignore Perimeter). That means we have to change how the area is calculated by Overriding the Area function. To be able to do this we have to make a slight modification to the Area function in Square. In order to override something that something must be declared "Overridable" (keep that in mind when you create your own classes).

Public Overridable Function Area() As Double
Return mdblLength ^ 2
End Function

Now we can override Area in our Cube class but first I have to explain something, the difference between Me, MyClass and MyBase.

Me
Me refers to the current object. You can use it to access the properties and methods of the class (ie: Me.Something()). Also to access class level variables that have been declared by the same name in a function/sub/property. ie

Private CurrentValue As String
Public Sub SetValue()
Dim CurrentValue As String
CurrentValue = "This is the local variable"
Me.CurrentValue = "This is the class variable"
End Sub

MyClass
Me works fine in most cases but not when it comes to inheritance (which is why I am explaining this now). As I mentioned earlier Cube needs to override the Area function of Square. Incase of overrides now if you called Area within Square it will call the overridden function in Cube. (I will give an example later in this tutorial)

MyBase
MyBase refers to the Parent, which the class it inherits from (in our case Square). I will use an example that we will be using in the Cube class. We need to calculate the area of a cube and what is the area of a cube? Length2 * 6 (six sides of a cube) or the area of a square times 6. Since Square already calculates the area we just have to multiply that by 6. So we need to override the Area function in the Cube class.

Public Overrides Function Area() As Double
Return MyBase.Area() * 6
End Function

Now you want an example of MyClass right? Well to do this we need to make a call to Area from within Square which we don't currently have. So make a function that returns the call to Area.

Public Function CallArea() As Double
Return Me.Area()
End Function

Now inside Sub Main in the module put the following:

Dim c As New Cube()
c.Length = 10
Console.WriteLine("Area is " & c.CallArea().ToString())

When you run the program you will get "Area is 600" not 100 (10 * 10) like you would expect. Even with the Me.Area() call it still calls the overridden function in Cube. If this isn't what you want to happen then you need to use MyClass.Area(). Now it will return 100 because it is calling the overridable function in Square.

Once you are satisfied with how it works you can remove it if you like.

Now on to the final part of the Cube class. Cube is a three dimensional shape and what do 3D shapes have? Volume. To calculate the volume of a cube it is simply Length3.

Public Function Volume() As Double
Return Length ^ 3
End Function

Time to test! Notice that you can declare a Square variable and set it to a new Cube object. When you call the Area function it still calls the Area function of the Cube so you get the correct result. This works just like it did with the FourSidedShape interface. The variable holds a Cube object but the type is Square so it can access all the properties and methods of the Square class. Again to be able to call the Volume function you have to cast it.

Sub Main()
Dim c As Square
c = New Cube(10)
Console.WriteLine ("Area is " & c.Area().ToString())
Console.WriteLine ("Volume is " & CType(c, Cube).Volume.ToString())
End Sub

A couple notes before we finish. No unfortunately VB.NET does not support multiple inheritance. If you want to overload (same name different parameter list) an overridable sub/function/property it must be declared as "Overloads". Where a function that isn't overridden (like Volume) does not have to be declared "Overloads".

Public Overloads Overrides Function Area() As Double
Return MyBase.Area() * 6
End Function

Public Overloads Function Area(ByVal TheLength As Double) As Double

End Function

Public Function Volume() As Double
Return Length ^ 3
End Function

Public Function Volume(ByVal TheLength As Double) As Double

End Function

Also note that an Interface can inherit another Interface and can actually inherit many other Interfaces unlike classes. If you implement an Interface that inherits from one or more other Interfaces then you must implement all properties and methods of all the interfaces.













 


© 2008-2009 dotnet4all.com