Visual Basic 2008 Tutorial

Lesson 4: Object Oriented Programming

 

Custom Search

Previous Lesson<<  [Home ]  >>Next Lesson

In first three lessons, you have learned how to enter the program code and run the sample VB2008 programs but without much understanding about the logics of VB2008 programming. Now, let’s get down to learning a few basic rules about writing the VB2008 program code.

First of all, let me say that though VB2008 is very much similar to VB6 in terms of Interface and program structure, their underlying concepts are quite different. The main different is that VB2008 is a full Object Oriented Programming Language while VB6 may have OOP capabilities, it is not fully object oriented. In order to qualify as a fully object oriented programming language, it must have three core technologies namely encapsulation, inheritance and polymorphism. These three terms are explained below:

Encapsulation refers to the creation of self-contained modules that bind processing functions to the data. These user-defined data types are called classes. Each class contains data as well as a set of methods which manipulate the data. The data components of a class are called instance variables and one instance of a class is an object. For example, in a library system, a class could be member, and John and Sharon could be two instances (two objects) of the library class.


Inheritance
Classes are created according to  hierarchies, and inheritance allows the structure and methods in one class to be passed down the hierarchy. That means less programming is required when adding functions to complex systems. If a step is added at the bottom of a hierarchy, then only the processing and data associated with that unique step needs to be added. Everything else about that step is inherited. The ability to reuse existing objects is considered a major advantage of object technology.

 

Polymorphism
Object-oriented programming allows procedures about objects to be created whose exact type is not known until runtime. For example, a screen cursor may change its shape from an arrow to a line depending on the program mode. The routine to move the cursor on screen in response to mouse movement would be written for "cursor," and polymorphism allows that cursor to take on whatever shape is required at runtime. It also allows new shapes to be easily integrated.

 

VB6 is not a full OOP in the sense that it does not have inheritance capabilities although it can make use of some benefits of inheritance. However, VB2008 is a fully functional Object Oriented Programming Language, just like other OOP such as C++ and Java. It is different from the earlier versions of VB because it focuses more on the data itself while the previous versions focus more on the actions. Previous versions of VB are  procedural or functional programming languages without OOP capabilities. Some other procedural programming languages are C, Pascal and Fortran.

VB2008 allows users to write programs that break down into modules. These modules will represent the real-world objects and are known as classes or types. An object can be created out of a class and it is known as an instance of the class. A class can also comprise subclass. For example, apple tree is a subclass of the plant class and the apple in your backyard is an instance of the apple tree class. Another example is  student class is a subclass of the human class while your son John is an instance of the student class.

A class consists of data members as well as methods. In VB2008, the program structure to define a Human class can be written as follows:

 

Public Class Human

'Data Members

Private Name As String

Private Birthdate As String

Private Gender As String

Private Age As Integer

 

'Methods

Overridable Sub ShowInfo( )

MessageBox.Show(Name)

MessageBox.Show(Birthdate)

MessageBox.Show(Gender)

MessageBox.Show(Age)

End Sub

End Class

 

After you have created the human class, you can create a subclass that inherits the attributes or data from the human class. For example, you can create a students class that is a subclass of the human class. Under the student class, you don't have to define any data fields that are already defined under the human class, you only have to define the data fields that are different from an instance of the human class. For example, you may want to include StudentID and Address in the student class. The program code for the StudentClass is as follows:

Public Class Students

Inherits Human

 

Public StudentID as String

Public Address As String

 

Overrides  Sub ShowInfo( )

MessageBox.Show(Name)

MessageBox.Show(StudentID)

MessageBox.Show(Birthdate)

MessageBox.Show(Gender)

MessageBox.Show(Age)

MessageBox.Show(Address)

End Sub

 

 

 

We will discuss more on OOP in later lessons. In the next lesson, we will start learning simple programming techniques in VB2008

 

 

 

  Bookmark and Share

 

Previous Lesson<<  [Home ]  >>Next Lesson

Copyright® Dr.Liew Voon Kiong  Contact: admin@vbtutor.net Privacy Policy]