Inheritance
Two of the benefits of OT are code reusability
and extensibility, and inheritance allows the implementation of both of these
features. When new objects are created they can inherit the data attributes or
variables from their class and all classes above them in the class hierarchy.
Because a method is procedural code, when an object inherits methods, it is
inheriting the programming code associated with the method.
Inheritance is an easy concept to understand,
and also a very powerful concept too, when a class is broken down into another
class or classes the class that was broken down is called the superclass and the
resulting class or classes are called subclasses. These subclasses then inherit
all of the methods and variables that the superclass has. By using inheritance
we do not have to repeat the methods or variables in each of the subclasses and
therefore do not have to produce any programming code in the subclass for the
inherited methods.
To illustrate inheritance, consider the
following example. In figure 1-9 a Jet object would inherit all variables from
the AIRPLANE class, including wing span, and number of engines data fields. In
addition, the jet object would inherit all of the variables from the VEHICLE
class, including serial number, weight, cost, and number of passengers.
Figure 1.9 A sample inheritance hierarchy
There are two types of inheritance in OT, single
inheritance and multiple inheritance. Single inheritance means that a subclass
has only one superclass, while multiple inheritance allows a subclass to have
more than one superclass. Up to now we have discussed only single inheritance.
Multiple inheritance is a very complicated concept that is not very well
understood. Some object-oriented languages such as Smalltalk do not support
multiple inheritance. In figure 1-9 if we were to add a SEAPLANE class, it
would be part boat and part airplane, so it would inherit methods and variables
from both the BOAT and AIRPLANE class.
Some database systems such as relational
databases do not support inheritance, but the object/relational databases will
soon support inheritance. More on this later in this chapter.
When a method is requested,
there are two ways that the method can be delivered. With late binding, the
databases will start at the class definition, and move up the class hierarchy
until the method is found. With early binding, the programmers specifies the
class where the method is located. The use of early binding will disable method
inheritance, since the object will only e search the target class when looking
for the method.
Important facts about Inheritance
It is critical to the
understanding of inheritance to note that inheritance happens at different times
during the life of an object.
-
Inheritance of data structures--At object creation
time, inheritance is the mechanism whereby the initial data structure for the
object is created. It is critical to note that only data structures are
inherited--never data. It is a common misconception that data is inherited,
such that an order may inherit the data items for the customer that placed the
order. We must understand that inheritance is only used to create the initial,
empty data structures for the object. In our example, all vehicles would
inherit data definitions in the vehicle
class, while an object of a lower-level class (say, sailboat)
would inherit data structures that only apply to sailboats--as in sail_size.
-
Inheritance of methods--Inheritance also happens
at runtime when a call to a method (stored procedure) is made. For example,
assume that the following call is made to sailboat object:
SAILBOAT.compute_rental_charges();
The database will first
search for the compute_rental_charges
in the sailboat class; if it is not found, the database will search
up the class hierarchy
until compute_rental_charges
is located.
Polymorphism
Polymorphism is the ability of different objects
to receive the same message and respond in different ways. Polymorphism
simplifies the communication between objects by using a common interface to hide
different implementation details. Polymorphism adds a high degree of
flexibility to OT by allowing new objects to be added to a computer system
without modifying existing procedures, helping to reduce maintenance and
allowing systems to change more rapidly as customer needs change.
As an example of how the same message can have
different behaviors, think of the event PROMOTION at work, this event will have
different behaviors based on the class of EMPLOYEE that receives the promotion.
A MANAGEMENT option might receive stock options and a bonus but a PROGRAMMER
object might receive a new title called senior programmer and a two percent
raise.
Polymorphism was a spin off of a programming
concept called ?overloading? Overloading is basically the ability of a
programming method to perform more than one kind of operation depending upon the
context in which the method is used. A very simple example of this is the use
of the operator ?+?, in working with characters it can be used for concatenation
and if used with numerical values it would be used for addition. A programming
example would look as follows.
Concatenation example:
First_Name = ?John?
Last_Name = 'smith?
Full_Name = First_Name + Last_Name
Numerical example
Kounter = Kounter + 1
In figure 1-9 both classes AUTOMOBILE, and
VEHICLE have a method called compute cost, this is referred to as overloading.
If we wanted to compute the cost of an automobile the method in the AUTOMOBILE
class would do the calculation. However for boats and airplanes the cost would
be computed in the VEHICLE class. The reason for putting a compute cost in the
AUTOMOBILE class is that the calculations are different for a boat than for an
airplane.
But the system does not care it simply will
compute cost based on the way compute cost is defined..
Extensibility
Extensibility is an especially powerful concept
that allows an object-oriented database to handle novel data situations easily.
Extensibility is the ability of an object-oriented system to add new behaviors
to an existing system without changing the application shell.
For example, consider a database called
customer. The database has pre-existing definitions of the CUSTOMER class and
also has two defined objects within this class, called NEW_CUSTOMER, and
NORMAL_CUSTOMER.
Lets assume
that we want to create a new object, FAVORED_CUSTOMER. An object-oriented
database would allow us to create a new class definition, specifying the unique
data attributes of this class, and creating the behaviors for FAVORED_CUSTOMER(s).
Once defined, FAVORED_CUSTOMER objects can be created and referenced in the same
fashion as any other objects within the database schema.