0% found this document useful (0 votes)
36 views7 pages

637680381469967353unit3 Se Chap1 Bca

Uploaded by

vishwasmv2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views7 pages

637680381469967353unit3 Se Chap1 Bca

Uploaded by

vishwasmv2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Object Orientation

We will approach the design of programs from an object-oriented perspective.

Key idea (notion?) in object orientation:


The real world can be accurately described as a collection of objects that interact.

Assumptions:

1. Describing large, complex systems as interacting objects make them easier to


understand than otherwise.
2. The behaviors of real world objects tend to be stable over time.
3. The different kinds of real world objects tend to be stable. (That is, new kinds appear
slowly; old kinds disappear slowly.)
4. Changes tend to be localized to a few objects.

Assumption 1 simplifies analysis, design, and implementation--makes them more reliable.

Assumptions 2 and 3 support reuse of code, prototyping, and incremental development.

Assumption 4 supports design for change.

The object-oriented approach:

 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

Our object model includes four components:

1. objects (i.e., abstract data structures)


2. classes (i.e., abstract data types)
3. inheritance (hierarchical relationships among ADTs)
4. polymorphism by inheritance

Objects

Objects are characterized by:

 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.

 It can change over time.


 It changes as the result of an operation performed on the object.
 It cannot change spontaneously.

The state is encapsulated within the object--is not directly visible.


operation:
a procedure that takes the state of the object and zero or more arguments and changes the
state and/or returns one or more values. Objects permit certain operations and not others.
identity:
a way to distinguish between two distinct objects (even if they have the same state and
operations).

As an example, consider an object for a student desk in a simulation of a classroom. The


relevant state might be attributes like location, orientation, person using, items in basket, items on
top, etc. The relevant operations might be state-changing operations (mutators) such as "move"
the desk, "seat student", or "remove from basket" or might be state-observing operations
(accessors) such as "is occupied" or "report items on desktop". A language is object-based if it
supports objects as a language feature.Object-based languages include Ada, Modula, Clu, C++,
Java, and Smalltalk. Pascal (without module extensions), Algol, Fortran. and C are not inherently
object-based.

Classes

A class is a template for creating objects.

 A class describes a collection of related objects (i.e., instances of the classes).


 Objects of the same class have common operations and a common set of possible states.
 The concept of class is closely related to the concept of abstract data type that we discussed
previously.

A class description includes definitions of

 operations on objects of the class,


 the possible set of states.

As an example, again consider a simulation of a classroom. There might be a class of "student


desks" from which specific instances (objects) can be created as needed. An object-based language
is class-based if the concept of class occurs as a language feature and every object has a class.
Class-based languages include Clu, C++, Java, Smalltalk, and Ada 95. Ada 83 and Modula are not
class-based.

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".

furniture <-- desk <-- student_desk

The simulation might also include a "computer desk" class that also derives from "desk".

furniture <-- desk <-- computer_desk

In Java, we can express the above inheritance relationships using the extends keyword as follows:

class Furniture // extends Object by default


{ ...
}

class Desk extends Furniture


{ ...
}

class StudentDesk extends Desk


{ ...
}

class ComputerDesk extends Desk


{ ...
}
Both "student desks" and "computer desks" will need operations to simulate a "move" of the entity
in physical space. The "move" operation can thus be implemented in the "desk" class and shared
by objects of both classes. Invocation of operations to "move" either a "student desk" or a
"computer desk" will be bound to the general "move" in the "desk" class.

The "student desk" class might inherit from a "chair" class as well as the "desk" class.

furniture <-- chair <-- student_desk

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).

class StudentDesk extends Desk, Chair // NOT VALID in Java


{ ...
}

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
{ ...
}

interface Seat extends Location3D, 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
{ ...
}

interface BookBasket extends Location3D, BookHolder


{ ...
}

class StudentDesk extends Desk implements Seat, BookBasket


{ ...
}

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.

Overloading (or ad hoc polymorphism)


means using the same name (or symbol) for several different procedures or functions (i.e.,
operations) and allowing the compiler to choose the correct one based on the signatures
(i.e., on the number, types, and order of the parameters).
void move () { ... }

void move (Location l) { ... }

void move (Desk d) { ... }

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.

As an example, again consider the simulation of a classroom. As in our discussion of


inheritance, suppose that the "student desk" and "computer desk" classes are derived from
the "desk" class and that a general "move" operation is implemented as a part of the "desk"
class. This could be expressed in Java as follows:

class Desk extends Furniture


{ ...
public void move(...)
...
}

class StudentDesk extends Desk


{ ...
// no move(...) operation here
...
}

class ComputerDesk extends Desk


{ ...
// no move(...) operation here
...
}

As we noted before, invocation of operations to "move" either a "student desk" or a


"computer desk" will be bound to the general "move" in the "desk" class.

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.

In Java, this can be expressed as follows:

class Desk extends Furniture


{ ...
public void move(...)
...
}

class StudentDesk extends Desk


{ ...
// no move(...) operation here
...
}

class ComputerDesk extends Desk


{ ...
public void move(...)
...
}

A class-based language is object-oriented if class hierarchies can be incrementally defined


by an inheritance mechanism and the language supports polymorphism by inheritance
along these class hierarchies. Object-oriented languages include C++, Java, Smalltalk, and
Ada 95. The language Clu is class-based, but does not include an inheritance facility. Other
object-oriented languages include Objective C, Object Pascal, Eiffel, and Oberon 2.

You might also like