Review of Object-Oriented Concepts

Team-Fly    

 
Application Development Using Visual Basic and .NET
By Robert J. Oberg, Peter Thorsteinson, Dana L. Wyatt
Table of Contents
Chapter 5.  Inheritance and Exceptions in VB.NET


In this preliminary section we review the fundamentals of object-oriented programming. If you are an experienced C++ or Java programmer, you may skim through this section as a refresher and begin your careful reading with the next section, where we introduce the case study. If you are primarily a Visual Basic programmer, then reading this section is recommended.

Objects

Objects have both a real-world and a software meaning. The object model describes a relationship between them.

Objects in the Real World

The term object has an intuitive real-world meaning. There are concrete, tangible objects, such as a ball, an automobile, and an airplane. There are also more abstract objects that have a definite conceptual meaning, such as a committee, a patent, or an insurance contract.

Objects have both attributes (or characteristics) and operations that can be performed upon them. A ball has a size , a weight, a color , and so on. Operations may be performed on the ball, such as throw, catch, and drop.

There can be various types of relationships among classes of objects. One, for example, is a specialization relationship, such as an automobile is a specific kind of vehicle. Another is a whole/part relationship, such as an automobile consists of an engine, a chassis, wheels, and other parts .

Object Models

Objects can also be used in programs. Objects are useful in programming because you can set up a software model of a real-world system. Software objects abstract the parts of objects in the real world that are relevant to the problem being solved . The model can then be implemented as software using a programming language. A software system implemented in this way tends to be more faithful to the real system, and it can be changed more readily when the real system is changed.

There are formal languages for describing object models. The most popular language is UML (Unified Modeling Language), which is a synthesis of several earlier modeling languages. Formal modeling languages are beyond the scope of this book, but we will find that informal models are useful.

Reusable Software Components

Another advantage of objects in software is that they can facilitate reusable software components. Hardware has long enjoyed significant benefits from reusable hardware components. For example, computers can be created from power supplies , printed circuit boards, and other components. Printed circuit boards in turn can be created from chips. The same chip can be reused in many different computers, and new hardware designs do not have to be done from scratch.

With appropriate software technology, similar reuse is feasible in software systems. Objects provide the foundation for software reuse.

Objects in Software

An object is a software entity containing data (state) and related functions (behavior) as a self-contained module. For example, a HotelBroker object may contain a list of hotels (the state) and provide operations to add a hotel and make a reservation (behavior).

Abstraction

An abstraction captures the essential features of a real-world object, suppressing unnecessary details. All instances of an abstraction share these common features. Abstraction helps us deal with complexity. For example, consider the problem of booking a reservation. There are many different kinds of things you might want to reserve, such as a hotel, an airplane flight, or a conference room. Such "reservables" have many differences, but they have certain essentials in common, such as a capacity.

Encapsulation

The implementation of an abstraction should be hidden from the rest of the system, or encapsulated . For example, the list of hotels may be contained in several different kinds of data structures, such as an array, a collection, or a database. The rest of the system should not need to know the details of the internal representation.

Classes

A class is a template for objects with common behavior and common structure. A class allows creation of new objects of the same type. An object is an instance of some class. We refer to the process of creating an individual object as instantiation .

Classes can be related in various ways, such as by inheritance and by containment .

Inheritance

Inheritance is a key feature of the object-oriented programming paradigm. You abstract out common features of your classes and put them in a high-level base class. You can add or change features in more specialized derived classes, which "inherit" the standard behavior from the base class. Inheritance facilitates code reuse and extensibility.

Consider Reservable as a base class, with derived classes Hotel and Flight . All reservables share some characteristics, such as a capacity. Different kinds of reservables differ in other respects. For example, a hotel has a city and a name , while a flight has an origin and a destination. Figure 5-1 illustrates the relationship among these different kinds of reservables.

Figure 5-1. Inheritance relationship among different reservable classes.

graphics/05fig01.gif

Abstract Classes

Sometimes a class is not meant to be instantiated , but only to provide a template for derived classes. The Reservable class is an exampleit is too abstract to actually instantiate. Only specific kinds of reservable classes, such as Hotel and Flight , may be instantiated. We call a class such as Reservable that cannot be instantiated an abstract class. A class that can be instantiated is called a concrete class.

Relationships Among Classes

Classes may be related to each other in various ways.

  • The inheritance (IS-A) relationship specifies how one class is a special case of another class. A Hotel (subclass or derived class) is a special kind of Reservable (superclass or base class).

  • The composition (HAS-A) relationship specifies how one class (the whole) is made up of other classes (the parts). A HotelBroker (whole) has a list of Hotel objects.

  • A weaker kind of relationship (USES-A) can be identified when one class merely makes use of some other class by calling on its methods to carry out work.

Polymorphism

Consider the problem of generating a payroll for various categories of employees. Different kinds of employees may have their pay calculated in a different manner. A salaried employee receives a fixed salary. A waged employee is paid according to the number of hours worked. A sales employee is paid according to the commissions earned on sales that were made.

A traditional approach is to maintain a type field in an employee structure and to perform processing in a Select Case statement, with a Case for each type of employee. Such use of Select Case statements is error prone and requires much maintenance when adding a new employee type.

An alternative is to localize the intelligence to calculate pay in each employee class, which will support its own specialized GetPay method. Generic payroll code can then be written that will handle different types of employees and will not have to be modified to support each additional employee type. Provide a GetPay method in the base class and an override of this method in each derived class. Call GetPay through an object reference to a general Employee object. Depending on the actual employee class referred to, the appropriate GetPay method will be called.

The ability for the same method call to result in different behavior depending on the object through which the method is invoked is referred to as polymorphism . Polymorphism can greatly simplify complex systems and is an important part of the object-oriented paradigm.

You should not try to coerce your design so that you can take advantage of polymorphism. We will see in our Acme Travel Agency case study that we have three different abstract base classes, but we do not need polymorphism to achieve quite general behavior. On the other hand, the .NET Framework classes use polymorphism heavily, as we shall see beginning in Chapter 6. Later in this chapter we will provide a small example of polymorphism using an employee class hierarchy, as outlined above.


Team-Fly    
Top
 


Application Development Using Visual BasicR and .NET
Application Development Using Visual BasicR and .NET
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 190

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