3.1. Principles of Object-Oriented Programming
Object-oriented programming is a software development architecture that uses the objecta "black box" of data and
3.1.1. Objects and ClassesAn object is a software-based collection of data elements and related procedures that act on those data elements. Obviously, objects are the central theme of "object-oriented" programming. In Visual Basic and other similar OOP languages, a class is the source code design of an object. An object is an in-memory instance of a class in a running program. Multiple object instances based on a single class can exist in memory at the same time.
Although the terms "class" and "object" have distinct meanings, the terms are used somewhat interchangeably in this chapter, at least in those cases where the distinction is not
3.1.2. AbstractionAn abstraction is a view of an entity that includes only those aspects that are relevant for a particular situation. It takes something from the real worldan employee, a book, a chart of accounts, a galaxy, a grain of sandand breaks it down into individual elements that can be managed with software. Consider a software component that provides services for tracking an employee's information. The first step in designing such a component is to identify the items or features that would be managed by the component. Some of these items may be:
This list includes not only basic data values, or
properties
, but also common actions to be taken on the data, or
The properties and methods of a class are relevant to that class. Although the Employee class could have included properties for IQ or the number of hairs on the employee's head, these data values have no relevance to the purpose of the class. Even though they are part of each employee, they provide no value to the class and are therefore excluded.
In short, the true employee has been abstractedthe class includes only those properties and methods of
3.1.3. Encapsulation
Encapsulation
is the process of converting an abstraction into a usable software componentthe black boxand exposing to the public only those portions of the abstraction that are
Encapsulation serves three useful purposes:
High-level programming languages already perform some encapsulation to simplify the work required by the programmer. For instance, the
SByte
data type, introduced in Visual Basic 2005, is an 8-bit integer data type that supports a range of numbers from -128 to 127. But how exactly does it record those 128 negative
Visual Basic uses a system named two's-complement representation to handle negative numbers. Basically, any time the leftmost bit is set to 1, the number is negative. Then there are various rules used to interpret the remaining bits, depending on whether the leftmost bit is set or not.
Do you want to know those rules? Do you really need to know those rule, or how negative values are managed at all? The great answer is: no! In most programming, you don't have to worry about how Visual Basic stores negative numbers at the binary level. Who cares? You only need access to negative numbers, not to the complex rules about how they are
Moreover, encapsulation protects programmers from making errors. For instance, if every programmer had to do the
Encapsulation has yet another important feature. Any code written using the exposed interface of the SByte data type remains valid, even if the internal workings of the SByte data type are changed for some reason. If Microsoft decided to have the SByte data type use one's complement representation (another method for managing negative numbers), it wouldn't matter to programs that used SByte , as long as the interface to the data type did not change. 3.1.4. InheritanceInheritance makes it possible for OOP code to build classes that extend or restrict features in other existing classes, without the need to fully rewrite the original class. For instance, a class of Pet may have generic data fields such as Name , Age , and Color . This single class could be extended into other, more specific classes through inheritance . A class named Dog that is derived from Pet would automatically include the Name , Age , and Color members, but it may add additional canine-specific members such as Breed and a ShedsHair flag. In this situation, the Dog class inherits from the Pet class.
Inheritance used in this manner
Some languages allow a class to inherit from multiple base classes at the same time. Visual Basic does not support this feature. 3.1.5. Interfaces
The public members of an object are known as its
interface
(or
public interface
). Usually, an object has a single public interface, since its class was designed with a single purpose in mind. But sometimes it is useful for a class to have multiple interfaces . For instance, along with the
Pet
class, consider another class called
House
. These two classes have some common aspects and
Interfaces are simply templates of desired functionality. To make these templates a functional reality, they must be implemented through a class. Implementing is a little different from inheriting. With inheritance, the new class receives the existing functionality of the base class; the new class doesn't have to reinvent this functionality. When implementing an interface, the new class is responsible for providing all of the functionality of the interface. While Visual Basic classes cannot inherit from multiple base classes, a single class can implement multiple interfaces at the same time. 3.1.6. Polymorphism
The
Sound confusing? Welcome to polymorphism. Fortunately, the Visual Basic compiler figures out all of these relationships for you; you just need to write your code to enable the class-specific actions you require. 3.1.7. Overloading
Sometimes it is useful to have more than one way of performing the same action in a single class. For instance, if your
Dog
class has a
TakeForWalk
action, you might require several ways of taking this action to
When a class includes multiple versions of the same member that
New in 2005 . The original .NET release of Visual Basic did not include operator overloading . This form of overloading allows you to provide custom meanings to the standard language operator symbols, such as the + (addition) and <> (not equal to) operators. The 2005 release of Visual Basic adds this form of overloading to the language. See the Chapter 5 " section of Chapter 5 for information on this enhancement, including examples. |