Flylib.com

Books Software

 
 
 

Abstraction

 < Day Day Up > 


Abstraction

VB has supported abstraction since version 4, and it is actually a simple concept. It is a view of an entity that includes only those aspects that are relevant for a particular situation. In other words, it is the ability of a language to create 'black box' code that takes a concept and creates an abstract representation of that concept within a program.

For instance, suppose we want to create the code that provides services for keeping an employee information database. We'll need to store the following list of items:

  • Name

  • ID

  • IncreaseSalary

  • DecreaseSalary

  • Salary

One of the most important ideas to keep in mind is that we included several items in this list for basic information, such as Name and ID. However, we also have an action entity that will be used to increase or decrease salary. These actions are referred to as methods in VB .NET. In pseudocode, the object takes the following form:

Employee Object Name ID IncreaseSalary() DecreaseSalary() Salary End Employee Object



 < Day Day Up > 
 < Day Day Up > 


Encapsulation

Encapsulation is the process of taking the abstract representation that we create and encapsulating the methods and properties, exposing only those that are totally necessary from a programmer's standpoint. The properties and methods of the abstraction are known as a member of the abstraction. The entire set of the members is known as the interface.

Simply put, this allows a developer to control the methods and properties that are available outside the object.



 < Day Day Up > 
 < Day Day Up > 


Inheritance

VB .NET is the first version of VB that supports inheritance, which is the idea that a class can gain the preexisting interface and behaviors of an existing class. In other words, a class can inherit these behaviors from the existing class through a process known as subclassing.

When an object is inherited, all of its properties and methods are automatically included in the new object. For example, suppose we look back at our Employee pseudocode object. Now, let's create a new object that tracks managers instead of employees :

Manager Object Inherits Employee Object ManagerPosition End Manager Object

This new Manager object will now have the same properties as the Employee object (an ID, Name , etc.), but also includes a property to detail the position the manager holds. This is a very powerful way to share the code that you have already written instead of re-creating the code.



 < Day Day Up > 
 < Day Day Up > 


Polymorphism

Polymorphism was introduced with VB4. Polymorphism simply means having or passing through many different forms. That is, it is the ability to write one routine that can operate on objects from more than one class, while treating different objects from different classes in exactly the same way. An easy example would be to create several classes that inherit the Name property from the Employee class we have been looking at. Basically, this means that the Name property would be available in many different forms.

Polymorphism allows an inherited method to be overridden. Again, looking at the Employee object, we could create a new object that inherits the Employee object, but with a new method with the same name as the IncreaseSalary method we used earlier. Although the new object would then have the methods and properties of the original, because the new method exists, it executes it.



 < Day Day Up >