Chapter 3: Object Syntax Introduction


Overview

Visual Basic supports the four major defining concepts required for a language to be fully object-oriented:

  • Abstraction - Abstraction is merely the ability of a language to create “black box” code, to take a concept and create an abstract representation of that concept within a program. A Customerobject, for instance, is an abstract representation of a real-world customer. A DataTableobject is an abstract representation of a set of data.

  • Encapsulation - This is the concept of a separation between interface and implementation. The idea is that you can create an interface (Publicmethods, properties, fields, and events in a class), and, as long as that interface remains consistent, the application can interact with your objects. This remains true even when you entirely rewrite the code within a given method - thus, the interface is independent of the implementation. Encapsulation enables you to hide the internal implementation details of a class. For example, the algorithm you use to compute pi might be proprietary. You can expose a simple API to the end user, but hide all the logic used by the algorithm by encapsulating it within your class.

  • Polymorphism - Polymorphism is reflected in the ability to write one routine that can operate on objects from more than one class - treating different objects from different classes in exactly the same way. For instance, if both Customer and Vendor objects have a Name property, and you can write a routine that calls the Name property regardless of whether you’re using a Customer or Vendor object, then you have polymorphism.

    Visual Basic supports polymorphism in two ways - through late binding (much like Smalltalk, a classic example of a true object-oriented language) and through the implementation of multiple interfaces. This flexibility is very powerful and is preserved within Visual Basic.

  • Inheritance - Inheritance is the idea that a class can gain the preexisting interface and behaviors of an existing class. This is done by inheriting these behaviors from the existing class through a process known as subclassing.

Then next chapter discusses these four concepts in detail; this chapter focuses on the syntax that enables you to utilize these concepts.

Visual Basic is also a component-based language. Component-based design is often viewed as a successor to object-oriented design, so component-based languages have some other capabilities. These are closely related to the traditional concepts of object orientation:

  • Multiple interfaces - Each class in Visual Basic defines a primary interface (also called the default or native interface) through its Public methods, properties, and events. Classes can also implement other, secondary interfaces in addition to this primary interface. An object based on this class has multiple interfaces, and a client application can choose with which interface it will interact with the object.

  • Assembly (component) level scoping - Not only can you define your classes and methods as Public (available to anyone), Protected (available through inheritance), and Private (available only locally), but you can also define them as Friend - meaning they are available only within the current assembly or component. This is not a traditional object-oriented concept, but is very powerful when designing component-based applications.

In this chapter, you’ll explore the creation and use of classes and objects in Visual Basic. You won’t get too deeply into code, but it is important that you spend a little time familiarizing yourself with basic object-oriented terms and concepts.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net