COSC3306: Programming Paradigms Lecture 6: Object-Oriented Programming Specifications - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

COSC3306: Programming Paradigms Lecture 6: Object-Oriented Programming Specifications

Description:

... oriented programming allows one to construct programs the way we humans tend ... language provides facilities that aid the programmer to construct abstractions. ... – PowerPoint PPT presentation

Number of Views:490
Avg rating:3.0/5.0
Slides: 31
Provided by: darlene4
Category:

less

Transcript and Presenter's Notes

Title: COSC3306: Programming Paradigms Lecture 6: Object-Oriented Programming Specifications


1
COSC3306Programming ParadigmsLecture 6
Object-OrientedProgramming Specifications
  • Haibin Zhu, Ph.D.
  • Computer Science
  • Nipissing University (C) 2003

2
Specification of OOPL
  • The object-oriented programming allows one to
    construct programs the way we humans tend to
    think about things.
  • For example,
  • We tend to classify real-world entities such as
    vehicles, airplanes, ATM machines, and so on.
  • Take a class of vehicles, all vehicles have
    certain components, such as engine, wheels,
    transmissions, and so on.
  • All vehicles exhibit some kind of behavior, such
    as acceleration, deceleration, and turning.

3
How object-oriented model can be represented?
  • One way is that an object may include persistent
    data and several data types with their associated
    operations.

Figure 8.1 The Operation/Parameter model
4
Representation
  • In the following, the message Perimeter can be
    sent to an object, which will behave according to
    its own method for handling the message. In this
    Figure, Square represents an active object with
    both of its data attributes, w and s, having
    values. Objects of type triangle or square each
    have two methods, namely Perimeter and Area.

5
Figure 8.2 The Message/Object model
6
Representation
  • As a consequence of the above discussion, we can
    say that, the object-oriented approach provides a
    programming method that consists of objects
    sending messages to other objects.

7
Design Principles for OOP
  • The object-oriented programming model has as its
    fundamental entity the object, which consists of
    a local state and a set of actions that it can
    perform, called methods. These methods are
    activated when the object receives an appropriate
    message, in which the actions consist of
    modification of the state of the object and the
    sending of messages to other objects.
  • The local state structure of an object and the
    methods that it can perform are specified by the
    class, which defines the object. In this sense,
    an object is called an instance of its class.
  • A class is used as a syntactic mechanism in
    almost all object-oriented languages, in which an
    individual representative of a class is known as
    an instance.

8
Objects
  • A module that consists of a hidden variable
    together with a group of exported operations on
    that variable.
  • One way to view an object is as a pair consisting
    of behavior and state.
  • The behavior of a component is the set of actions
    it can perform. The complete description of all
    the behaviors for a component is sometimes called
    the protocol, in which includes all activities
    involved.
  • The state of a component represents all the
    information held within it. It is worth noting
    that, the state is not static and can be changed
    over time (dynamic).
  • The state is described by the instance variables,
    whereas the behavior is characterized by the
    methods, in which provide the appropriate
    behavior through modifications of the state as
    well as by interacting with other objects.

9
Classes
  • A class is a template for objects and contains of
    the following
  • Descriptions of the actions an object can
    perform.
  • Definition of the structure of an object.
  • In other words, a class must provide a means for
    defining the forms of objects. What data do they
    own? What can they do? How do they do things?
  • A class is similar to an abstract data type which
    defines the type data structure and procedures
    that apply to it.

10
Messages
  • It is often useful to think of computation in an
    object-oriented system as evolving around
    messages.
  • A message is a request transferred from one
    object to another in order the receiving object
    to produce some desired result. Using this
    analogy, objects in the system manipulate other
    objects by sending messages requesting them to
    perform specific action.

11
Unary Messages
  • Unary Messages have no parameters. They have only
    two parts, the object to which they are to be
    sent and the name of the method in that receiving
    object. In this respect, the first symbol of an
    unary message specifies a receiver object, and
    the second symbol specifies the method of that
    object that is to be executed. For example, the
    message
  • SecondChild Boy
  • sends a parameterless message to the Boy method
    of the object SecondChild. Recall that all
    objects are referenced by pointers, so
    SecondChild is really a pointer to an object.

12
Binary Messages
  • Binary Messages have a single parameter, an
    object, which is passed to the specified method
    of the specified receiver object. Among the most
    common binary messages are those for arithmetic
    operations. For example, in the message
  • 20 ? 10
  • the receiver object is the number 20, to which is
    sent the message ?10. In this respect, the
    message passes the parameter object 10 to the ?
    method of the object 20. The code of that method
    uses the object 10 to build a new object, in this
    case 30. If the system already contains the
    object 30, then the result is a reference to it
    rather than to a new object.

13
Keyword Messages
  • Keyword Messages specify one or more keywords to
    organize the correspondence between the actual
    parameters in the message and the formal
    parameters in the method. In other words, the
    keywords select the method to which the message
    is directed. Methods that accept keyword messages
    are not named, but instead are identified by the
    keywords themselves. For example, consider the
    following message
  • FirstArray Head 1 Tail 5
  • which sends the objects 1 and 5 to a particular
    method, Head and Tail , of the object
    FirstArray. The keywords Head and Tail
    identify the formal parameters of the method to
    which 1 and 5, respectively, are to be sent. In
    this respect, the method to which this message is
    sent includes the keywords of the message.

14
Methods
  • A method consists of the operations that an
    object performs when it receives a message. A
    one-to-one correspondence between messages and
    the methods that are executed when a given
    message is received by an object of a particular
    class.
  • Objects of different classes might perform
    different methods for the same message if that
    message were bound to a different method in the
    definition of the two classes.
  • In contrast, if a message corresponds to a
    procedure call, then the method corresponds to
    the procedure itself.

15
Example
  • An object-oriented model consisting of different
    objects to accomplish some methods of
    communication. Communication from an object
    Student to the object Financial-Aid, as indicated
    by arrow, is through the messages ID and
    Transaction. The Financial-Aid can respond by
    sending messages Money and Receipt. The object
    Financial-Aid is thought of as an active entity
    including data and also capable of sending
    messages, receipts, or money whereas algorithms
    manipulate passive data.

16
Figure 8.3 Object-oriented representation of
financial-aid-receiving problem
17
Implementing OOP
  • In general, an object-based language supports
  • Data Abstraction (Encapsulation of state with
    Operations)
  • Encapsulation (Information Hiding)
  • Polymorphism (Message Passing)
  • and a language that is object-oriented also
    implements
  • Inheritance, and
  • Dynamic Binding.
  • In this context, inheritance is the ability to
    organize object classes into a hierarchy of
    subclasses and superclasses, and for operations
    of a given class to be applicable to objects of a
    subclass. The key concepts that are common to the
    implementation of each object-oriented
    programming language are as the following.

18
Data Abstraction
  • One of the recent trends in programming
    methodology and programming languages has been
    the specification of Abstract Data Types (ADT).
  • That is, the programmer first decides what data
    structures and data types are required and then
    specifies the necessary operations in terms of
    their inputs and outputs. Finally, the
    representation of the data structures and
    operations is fixed addressing that the abstract
    data type is implemented. From the theoretical
    point of view, an abstract data type is defined
    as a collection of data structures and operations
    abstracted into a simple data type.

19
Definition of Abstraction
  • A set of data objects, ordinarily using one or
    more type definitions.
  • A set of abstract operations on those data
    objects.
  • When we design an abstract data type, we should
    make sure that the constructors (e.g., int and
    push) and selectors (operations implemented) work
    together smoothly. This is necessary if the data
    type is to be easy to learn and easy to use.

20
Languages support
  • The language directly supplies a useful set of
    abstractions that we think of as the features of
    the language.
  • The language provides facilities that aid the
    programmer to construct abstractions.
  • Subprograms, subprogram libraries, type
    definitions, classes and packages are some of the
    facilities provided by different languages to
    support programmer-defined abstractions.

21
Example
  • Consider a list of the grocery items. In this
    respect, grocery list contains items of the same
    type milk, eggs, butter, apples, bread, chicken,
    etc.
  • What can you do to the items on a list? You might
    count the items to determine the length of the
    list, add an item to the list, remove an item
    from the list, or look at an item. The items on a
    list together with operations that you can
    perform on the items form an abstract data type.

22
Operations of ADT List
  • Create an empty list.
  • Determine whether a list is empty.
  • Determine the number of items on a list.
  • Add an item at a given position in the list.
  • Remove the item at a given position in the list.
  • Remove all the items from the list.
  • Retrieve the item at a given position in the list.

23
Implement ADT
  • The first reaction to the implementation might be
    to choose a data structure and then to write
    methods that operate on it in accordance with the
    operations.
  • In a non-object-oriented implementation, both the
    data structure and the abstract data type
    operations are distinct pieces.
  • In contrast, object-oriented implementation
    provides encapsulation as a fundamental
    principles of abstract data type implementation
    which is the main focus of the next section.

24
Encapsulation
  • Encapsulation or information hiding is simply the
    idea of packaging things together in a
    well-defined programming unit. When information
    is encapsulated in an abstraction, it means that
    the user of the abstraction
  • Does not need to know the hidden information in
    order to use the abstraction, and
  • Is not permitted to directly use or manipulate
    the hidden information even if desiring to do so.
  • For example,
  • The integer data type in a programming language
    not only hides the details of the integer number
    representation, but also effectively encapsulates
    the representation so that the programmer cannot
    manipulate individual bits of the representation
    of an integer.
  • The class in C provides for encapsulation by
    packaging both data and function members into a
    single class unit.

25
Polymorphism
  • One of the most powerful capabilities of
    inheritance in object-oriented programming
    languages is its support for polymorphism, the
    ability to have classes in the same inheritance
    hierarchy respond differently to the same
    message.
  • Two categories
  • Pure polymorphism occurs when a single function
    can be applied to arguments (objects) of a
    variety of types. In pure polymorphism, there is
    one function (code body) and a number of
    interpretations.
  • Ad hoc polymorphism sometimes called overloading
    occurs when there are a number of different
    functions (code bodies) all denoted by the same
    name. This is in direct contrast to pure
    polymorphism, in which functions that are part of
    the same class have different signatures.

26
Inheritance
  • Inheritance is one of the most important
    properties of the object-oriented programming
    because of its support for reusability. This
    property applies to classes. For example, class A
    is said to inherit from class B if A is a
    subclass of B and B is a superclass of A.
  • The advantage of inheritance is that it permits
    the reuse of many of the features of a superclass
    without respecifying them.
  • The ability to modify or remove inherited
    features selectively makes inheritance much more
    powerful.
  • Inheritance may be either
  • Single if each class has just one parent
  • Multiple when classes are permitted to have more
    than one parent.

27
Figure 8.4 An inheritance graph for graphical
objects
28
Dynamic Binding
  • When a programming language determines how a
    particular operation is to be performed on an
    object, it is said to bind a specific
    implementation of that operation to the object.
  • In other words, the property of attaching a
    message to method when a message is sent is known
    as binding.
  • If the system decides on which implementation of
    the operation to use at compile time, it performs
    static binding.
  • This property is dynamic binding (sometimes
    called late binding) if it is carried out every
    time a message is forwarded, indicating that the
    choice is made at run time.

29
Desirable Characteristics for OOP
  • Close correspondence to the problem domain,
    meaning that the objects in programs correspond
    directly to objects in the problem domain, and
    their activities correspond as well.
  • Reusability of software, meaning that the
    inheritance of properties by one class of
    projects from another class without explicitly
    respecifying them.
  • Abstraction and encapsulation, meaning that the
    object-oriented model carries with it all the
    abstraction benefits of abstract data types by
    permitting the data and actions of an object to
    be encapsulated within that object and dividing
    its contents between those that are public and
    those that are private.
  • Collecting similar operations, meaning that
    similar operations from two different components
    are collected into a new component.
  • Concurrency, meaning that objects can act
    concurrently in the problem domain.

30
Summary
  • OOP
  • Objects (Abstraction)
  • Classes (ADT)
  • Inheritances (Classification)
  • Overloading (Polymorphism)
  • Dynamic Binding (Polymorphism)
Write a Comment
User Comments (0)
About PowerShow.com