Sunday, October 15, 2006

Inheritance in VB.NET

Sell and Buy Used Book - Bukz4u.com

What Is Inheritance?


Inheritance is a very powerful form of code reuse. With inheritance, you write one class that contains some properties and methods. That class becomes the basis for other classes, and is known as the base class. Classes that then use this base class are known as derived classes. Most often, the derived classes extend the functionality of the base class.

Inheritance is the mechanism which allows class A to inherit properties of class B. We say “A inherits from B”. Thus, objects of class A have access to attributes and methods of class B without the need to redefine them. The following definition defines two terms with which we are able to refer to participating classes when they use inheritance.

Superclass/Subclass

If class A inherits from class B, then B is called a superclass of A. A is called a subclass of B. Objects of a subclass can be used where objects of the corresponding superclass are expected. This is due to the fact that objects of the subclass share the same behavior as objects of the superclass.

Implementing Inhertance In VB.NET

When we say inheritance in Visual Basic .NET, we mean that a child gets a copy of everything found in a parent plus anything we add to that child. Technically, a child gets a copy of the data and access to a methods table, but for simplicity we think of child classes as getting their own copy of everything. This does not mean that a child can directly access every member inherited from the parent; access rules still apply. The child gets a copy of everything, but can only directly access public, protected, and friend members of a parent. Because private members represent implementation details, a child class is usually interacting with private members indirectly via public, protected, or friend members.

Basic Inheritance Statement

Fortunately the grammatical complexity of inheritance relationships is miniscule. Given two classes, one playing parent and the other child, all you need to do is add a single statement to indicate inheritance

The simplest inheritance relationship.


Public Class Parent

End Class

Public Class Child
Inherits Parent
End Class



From the above code you can see that Inherits Parent is all that we use to indicate inheritance of child from parent. The grammar is the easy part. Choosing what to expose in a parent and what to add to a child is the very subjective part of object-oriented programming that trips some programmers up.

Basic guidelines for Inheritance

Make methods protected and virtual unless you are reasonably sure they should not be.

When defining reference parameters, define parameters to be parents reasonably high up in the architecture. You can always pass an instance of a child and typecast the parameter.

When you have a class that works and needs new behavior, create a child class and add the behavior to the child. You will not break existing code by modifying an existing class. You will have the old and new behavior and two classes to draw from.

Keep methods and property methods very short and singular.

Inheritance Example

Create a new VB.NET Windows Application project and name it InheritanceTest. Add a button to the form and go to the code window. In the code, add the following class. Make sure that you add it outside the class for the form

Public Class Person
Dim localName, localAddress As String
Property Name() As String
Get
Name = localName
End Get
Set(ByVal Value As String)
localName = Value
End Set
End Property
Property Address() As String
Get
Address = localAddress
End Get
Set(ByVal Value As String)
localAddress = Value
End Set
End Property
Public Function Enroll() As Boolean
'code to check class enrollment , if enrollment Enroll = True
End Function
End Class


This code creates a Person class with two properties, Name and Address, and an Enroll method.

Now, add a second class, called Student. Your code should look like this:

Public Class Student
Inherits Person
End Class



There isn’t any implementation code in Student at all. There are no properties or methods defined. Instead, all you do is inherit from Person.

Now, in the Button1_Click event handler on the form, add the following code:

Dim Student As New Student()
MsgBox(Student.Enroll)

Your form is creating an instance of the Student class. The only code in the Student class is an Inherits statement that inherits the Person class. Since VB.NET supports inheritance, you’ll be able to call the Enroll method of the Person class from within the Student class, even though the Person class does not explicitly define an Enroll method. Instead, the Enroll method is present because Student is inheriting the method from Person.

Post your comments....

Next Blog on how to prevent and force inheritance...