No Slide Title - DePaul University

Download Report

Transcript No Slide Title - DePaul University

Chapter 1
Object-Oriented Programming
1
OO programming and design
• Object-oriented programming and design
can be contrasted with alternative
programming approaches and associated
design techniques.
• For example, object-oriented programming
and design can be contrasted with
procedural programming and top-down
functional decomposition design.
2
OO programming and design
• The contrast between object-oriented
programming and alternatives such as
procedural programming can be highlighted
by focusing on program modules.
• A module is a program component that can
be designed, coded, translated, and tested
independently and then incorporated into a
larger program.
3
Modules
• Modules in procedural languages such as C
are procedures, e.g., the C function
int find_max( int n1, int n2 ) {
if ( n1 > n2 ) return n1;
else return n2;
}
4
Modules
• Modules in object-oriented languages such
as Java are classes, which encapsulate or
contain storage variables and procedures.
– For example, Java has a String class that
encapsulates storage for the characters in a
string together with procedures for constructing
a string, determining a string’s length, checking
whether a string contains a given character, and
so on.
5
Top-down design
• Procedural programming is associated with
a design technique known as top-down
design or top-down functional
decomposition.
• In top-down design, a problem is
decomposed into subproblems. The problem
and its subproblems are then assigned to
procedures for solution.
6
Top-down design
– For example, the problem of building a car
might be decomposed into the subproblems of
first building the chassis, the engine, the
drivetrain, and so on and then of assembling the
prebuilt parts. The overall problem of building
the car could be assigned, in a simulation, to a
main procedure, which in turn would invoke
subprocedures such as build_chassis to
handle the subproblems.
7
Top-down design
• Top-down design and the associated
procedural programming have drawbacks,
particularly with respect to software
maintenance. For example, a change in
problem decomposition entails a change in
procedural decomposition, which can ripple
through an entire decomposition hierarchy.
This is known as cascading changes.
8
Top-down design
– For instance, suppose that the main procedure
(which handles the main problem of building a
car) needs to be changed by, for example,
passing a new argument to subprocedures such
as build_chassis, which in turn need to
pass the new argument to their subprocedures,
and so on until the change in main has rippled
throughout the decomposition hierarchy.
9
OO design and programming
• Object-oriented design and programming
attempt to overcome shortcomings such as
cascading changes.
• In an object-oriented approach, classes
behave as modules that can interact with
other modules in such a way that a change
to one does not require a change to all of the
others.
10
Two senses of class
• Class has two distinct but related senses:
– In object-oriented design, a class is a collection
of objects such as humans that share features or
properties (e.g., humans are warm-blooded and
risible) and behaviors or operations (e.g.,
humans gather, hunt, cook, and even tango). A
class Human could represent humans in an
object-oriented design.
11
Class as data type
– In object-oriented programming, a class is a
data type that can have instances. For example,
Java has a String class that is a standard data
type in the language. Particular strings such as
“Mary Leakey” would be represented in Java as
String instances or objects. A programmerdefined class such as Human might be
introduced as a data type to represent humans.
12
Class as data type
– The classes of object-oriented design can be
represented in an object-oriented language as
class data types.
– Class data types encapsulate variables to
represent class features (e.g., a variable
footCount might be initialized to 2) and
procedures to represent operations (e.g., a
procedure might show a video of two humans
doing the tango).
13
Class and encapsulation
• The class as a data type can encapsulate
– variables or fields, which represent features or
properties that class instances share
– procedures
• Procedures used to create or construct instances of a
class are known as constructors.
• Procedures that represent class operations or
behaviors are know as methods.
14
Class members
• Fields are encapsulated variables, including
arrays or other aggregates.
• Constructors are encapsulated procedures
used to construct class instances.
– In Java, the code segment
new String()
uses the operator new and the
String()constructor to construct a string.
15
Methods
• Methods are encapsulated procedures that
provide class operations.
– In Java, the code segment
String s = new String( “hi” );
int len = s.length();
first constructs a string and then invokes the
encapsulated length method to determine the
number of characters in the string.
16
“Class” and “instance” members
• The class data types in object-oriented
languages have two types of members:
– Members associated with the class are “class
members.” For example, an Emp class might
have a “class member” named count to track
how many Emp instances have been
constructed.
17
“Class” and “instance” members
– Members associated with class instances are
“instance members.” For example, an Emp
class might have a name “instance member” to
represent each Employee’s name.
– In Java, members marked as static are
“class members,” whereas members not marked
as static are “instance members.”
18
“Class” and “instance” members
– The Java code segment
class Emp {
static int count;
String name;
}
illustrates the syntax. The field count is a
“class member,” whereas the field name is an
“instance member.”
19
Class and abstract data type
• The class construct in object-oriented
languages directly supports abstract data
types—data types defined by high-level
operations rather than by low-level
implementation details.
• Use of abstract data rather than primitive
data types eases programming tasks.
20
Stack as an abstract data type
• A Stack is an example of an abstract data
type. A Stack is a list with insertions and
deletions done at the same end, known as
the top. Its high-level operations include
– push, which inserts an item onto the Stack.
– pop, which removes the Stack’s top item.
– peek, which shows the Stack’s top item without
removing it.
21
Stack class
• A Stack class type would encapsulate
methods such as push, pop, and peek to
represent high-level Stack operations.Other
methods such as isEmpty and isFull
could be included to test whether a Stack
is empty or full. Constructors would be
provided to construct Stack instances.
22
Information hiding
• An abstract data type exposes to clients a
high-level interface that specifies the type’s
behavior.
– In the Stack example, the interface consists of
methods such as push and pop that specify
high-level operations on a Stack.
• An abstract data type hides from clients the
type’s low-level, implementation details.
23
Information hiding
• In object-oriented languages, a class is a
type that supports information hiding.
– Class members such as high-level methods can
be declared public to expose such methods to
clients.
– Class members that provide implementation
detail can be declared private to hide such
details from clients.
24
Information hiding
• By directly supporting information hiding, a
class in an object-oriented language is well
suited for delivering abstract data types.
– The Java String class is, in effect, an
implementation of a string as an abstract data
type.
25
Client/server model and OO
• Object-oriented programming is based on a
client/server model of computing
– A class and its instances (objects) are servers
that provide services to clients.
– An application that uses a class and its
instances are clients.
– For instance, the Java String class provides
string-processing services to client applications.
26
Message passing
• In the client/server model, invoking a class
or instance method sends a message that
requests a service.
– For instance, the code segment
String s = “Hi, world!”;
int n = s.length();
requests the length of the string to which s refers.
27
Inheritance
• Object-oriented languages support
inheritance hierarchies, which are
parent/child relationships between classes.
– For instance, a Window class might have
DialogWindow as a subtype. In this case, the
Window class is the parent class and the
DialogWindow class is the child class.
28
Inheritance
• Inheritance provides a basic form of code
reuse in that a child class inherits parent
class members, including the methods.
• Some object-oriented languages (e.g., C++)
allow a child class to have multiple parent
classes. In Java, by contrast, every class
except Object has exactly one parent.
29
Inheritance
• Languages such as C++ thus support
multiple inheritance for classes, whereas
languages such as Java support only single
inheritance for classes.
• Designing inheritance hierarchies is a
critical part of object-oriented design.
30
Polymorphism
• Polymorphism is a powerful object-oriented
construct in which distinct methods within
an inheritance hierarchy can be invoked
with the same syntax.
– For instance, assume an inheritance hierarchy
of window classes in each class has its own
appropriate implementation of a show method,
which shows the window on the screen.
31
Polymorphism
– Regardless of whether win refers to, say, a
MenuWindow or a MessageWindow, the
statement
win.show(); // show yourself
displays the window. At run time, the systems
determines the type of window to which win
refers and invokes the show method
encapsulated in that type.
32
Polymorphism
• A method such as show is a polymorphic
method; that is, a method invoked with the
same syntax as other polymorphic methods
in the same inheritance hierarchy.
• Polymorphism is used widely in objectoriented programming because of its power,
flexibility, and convenience.
33
Component-based programming
• Modern object-oriented programs tend to be
component-based.
• A component is a prebuilt part or module
that can be integrated with other such parts
to build or extend an application.
• A container is a special-purpose component
that can hold or embed other components.
34
Component-based programming
• Programs with graphical user interfaces are
commonly component-based. For example,
the user interface might consist of a framed
window (container) that holds components
such as buttons, lists, checkboxes, menus,
and the like.
• Component-based programming is not
restricted to GUIs, however.
35
Component-based programming
• To be easily usable, a component should be
designed under object-oriented principles:
– The component should expose to clients only
high-level functionality.
– The component should hide from clients the
low-level implementation details that support
its high-level functionality.
36
UML
• The Unified Modeling Language (UML)
facilitates the design, development,
deployment, and maintenance of software
systems.
• UML has become the modeling language of
choice for object-oriented design.
• UML is graphical, expressive, and concise.
37
UML
• Basic UML vocabulary consists of
– Things: Entities to be modeled, whether
concrete or abstract.
– Relationships: Connections among things.
– Diagrams: graphical depictions of things and
their relationships
38
UML diagram
University
1
*
Academic Department
39
UML diagram
• The diagram on the previous slide expresses
that a university is a collection of arbitrarily
many academic departments, each of which
is associated with exactly one university.
• UML diagrams can serve as high-level
design specifications for software systems
and, in this role, can guide code
development.
40