637680381469967353unit3 Se Chap1 Bca
637680381469967353unit3 Se Chap1 Bca
Assumptions:
Use the same basic entities (i.e., objects) throughout the lifecycle.
Identify basic objects during analysis.
Identify lower-level objects during design, reusing existing object descriptions where
appropriate.
Implement the objects as software structures (e.g., Java classes).
Maintain the object behaviors.
An Object Model
Objects
state,
operations,
identity.
An object is a separately identifiable entity that has a set of operations and a state that records the
effects of the operations. That is, an object is essentially the same as an abstract data structure as
we have discussed previously.
state:
the collection of information held (i.e., stored) by the object.
Classes
Inheritance
A class D inherits from class B if D's objects form a subset of B's objects.
Class D's objects must support all of the class B's operations (but perhaps are carried out
in a special way).
Class D may support additional operations and an extended state (i.e., more information
fields).
Class D is called a subclass or a child or derived class.
Class B is called a superclass or a parent or base class.
The importance of inheritance is that it encourages sharing and reuse of both design information
and program code. The shared state and operations can be described and implemented in base
classes and shared among the subclasses. As an example, again consider the student desks in a
simulation of a classroom. The "student desk" class might be derived (i.e., inherit) from a class
"desk", which in term might be derived from a class "furniture".
The simulation might also include a "computer desk" class that also derives from "desk".
In Java, we can express the above inheritance relationships using the extends keyword as follows:
The "student desk" class might inherit from a "chair" class as well as the "desk" class.
Some languages support multiple inheritance as shown above for "student desk" (e.g., C++, Eiffel).
Other languages only support a single inheritance hierarchy.
Because multiple inheritance is both difficult to use correctly and to implement in a compiler, the
designers of Java did not include a multiple inheritance of classes as feature. Java has a single
inheritance hierarchy with a top-level class named Object from which all other classes derive
(directly or indirectly).
To see some of the problems in implementing multiple inheritance, consider the above example.
Class StudentDesk inherits from class Furniture through two different paths. Do the data fields of
the class Furniture occur once or twice? What happens if the intermediate
classes Desk and Chair have conflicting definitions for a data field or operation with the same
name?
The difficulties with multiple inheritance are greatly decreased if we restrict ourselves to
inheritance of class interfaces (i.e., the signatures of a set of operations) rather than a supporting
the inheritance of the class implementations (i.e., the instance data fields and operation
implementations). Since interface inheritance can be very useful in design and programming, the
Java designers introduced a separate mechanism for that type of inheritance.
The Java interface construct can be used to define an interface for classes separately from the
classes themselves. A Java interface may inherit from (i.e., extend) zero or more other interfaces.
interface Location3D
{ ...
}
interface HumanHolder
{ ...
}
A Java class may inherit from (i.e., implement) zero or more interfaces as well as inherit from
(i.e., extend) exactly one other class.
interface BookHolder
{ ...
}
This definition requires the StudentDesk class to provide actual implementations for all the
operations from the Location3D, HumanHolder, BookHolder, Seat, and BookBasket interfaces.
The Location3D operations will, of course, need to be implemented in such a way that they make
sense as part of both the HumanHolder and BookHolder abstractions.
Polymorphism
The concept of polymorphism (literally "many forms") means the ability to hide different
implementations behind a common interface. Polymorphism appears in several forms.
The choice among the alternatives can be done statically at the time the program is
compiled. Java supports overloading of method calls, but does not support user-defined
overloading of operator symbols. C++ supports both.
Parametric polymorphism
denotes the use of parameterized type (or class) definitions.
Java does not currently support parameterized class definitions. The template facility in
C++ and the generic facility in Ada are examples of parametric polymorphism. There are
experimental extensions to Java that do support parametric polymorphism.
Polymorphism by inheritance (sometimes called pure polymorphism, but a subset of what Budd's
textbook calls pure polymorphism)
means the association at runtime (or the dynamic binding) of an operation invocation (i.e.,
procedure or function call) with the appropriate operation implementation in an inheritance
hierarchy.
This form of polymorphism is carried out at run time. Given an object (i.e., class instance)
to which an operation is applied, the system will first search for an implementation of the
operation associated with the object's class. If no implementation is found in that class, the
system will check the superclass, and so forth up the hierarchy until an appropriate
implementation is found. Implementations of the operation may appear at several levels of
the hierarchy.
Extending the example, suppose that we need a special version of the "move" operation for
"computer desks". For instance, we need to make sure that the computer is shut down and
the power is disconnected before the entity is moved. We can define this special version of
the "move" operation and associate it with the "computer desk" class. Now a call to "move"
a "computer desk" will be bound to the special "move" operation, but a call to "move" a
"student desk" will still be bound to the general "move" operation in the "desk" class. The
definition of move in ComputerDesk overrides the definition in Desk.