Top Banner

of 39

SE 03 OO Concepts [Read-Only]

Apr 03, 2018

Download

Documents

shamcs
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    1/39

    History

    Meta-model of Objects

    Objects & Messages

    Classes Inheritance

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    2/39

    Background

    Meta-Model

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    3/39

    - 3 - Prof. Dr. H. Lichter

    History of Object Orientation

    Simula: Ole-Johan Dahl and Krysten Nygaard, Norway,

    1967 A language for simulation (i.e. modeling)

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    4/39

    - 4 - Prof. Dr. H. Lichter

    Why objects?

    Finally I would like to try to answer the following fair

    question: How could it happen that a team of twoworking in the periphery of Europe could hit onprogramming principles of lasting importance?

    No doubt a bit of good luck was involved. We weredesigning a language for simulation modeling, and

    such models are most easily conceived of in terms ofcooperating objects.

    Our approach, however, was general enough to beapplicable to many aspects of system development.

    Ole-Joh an Dah l: s dm Con ferenc e Soft ware Pio neers , Bo nn , 2001

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    5/39

    - 5 - Prof. Dr. H. Lichter

    OO Languages - Milestones

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    6/39

    - 6 - Prof. Dr. H. Lichter

    Object based

    Objects

    CLU

    (Ada)

    (Modula-2)

    Ada

    Modula-2

    Simula

    Smalltalk-80Eiffel

    C++

    Java

    Modula-3

    Aspects of Object Oriented Languages

    Class based

    Objects

    Classes

    Object oriented

    Objects

    Classes

    Inheritance

    Objects are

    encapsulated

    data, described one

    by one

    Classes describe

    types of objects

    Data, objects can

    be derived from

    classes.

    Inheritance defines

    a specialization-

    hierarchy between

    classes

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    7/39

    - 7 - Prof. Dr. H. Lichter

    A Metamodel of Objects 1

    A metamodel of objects

    defines elements and relationships which can be used for modeling

    Basic elements are

    object and

    class

    Relationships are

    uses and

    inherits

    Additionally, rules fo r modeling are defined

    Different programming languages may define differentmetamodels

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    8/39

    - 8 - Prof. Dr. H. Lichter

    Ab stract Cla ss

    Method

    Ab stract Ob je ct

    Method

    Object AttributeClass Attribute

    Class Method

    0..*

    1..*

    0..*

    1..*

    accesses

    Ab stract Cla ss

    Object Method

    Ab stract Me th od

    1..*1..*

    specifies

    Attr ibute

    0..*1..* 0..*1..*

    accesses

    Class

    Program Module

    0..*

    0..*

    +Server0..*

    uses

    +Client

    0..*

    0..*

    0..*+Super

    0..*

    inherits+Sub

    0..*

    0..*0..*

    implements

    0..*0..*

    defines /

    redefines

    Object

    is_instance_of

    Method

    0..*0..*

    redefines

    Message

    sends

    receives

    triggers execution of

    A Metamodel of Objects 2

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    9/39

    Objects

    Messages

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    10/39

    - 10 - Prof. Dr. H. Lichter

    Objects in a Domain Perspective

    Objects are models of relevant things

    e.g. my account, your car, his house

    Things are characterized through their behavior andhow they can be dealt with

    Two basic questions:

    Which information can be derived? Which actions can be triggered?

    Objects

    encapsulate related information and behavior

    define an interface through these can be accessed

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    11/39

    - 11 - Prof. Dr. H. Lichter

    Objects in a Programming Perspective

    A program is made up of objects

    Each activity is performed by objects

    Objects

    have a lifetime (from creation to destruction)

    have an identity (ensured by the system)

    are active

    change through their lifetime

    send and receive messages

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    12/39

    - 12 - Prof. Dr. H. Lichter

    Inside an object

    An object consists of two parts

    Data - which represent the state of the object

    Methods - which can change the data (and only these!)

    Objects behave by executing their methods

    If an object receives a message the corresponding method willbe executed

    The state may change through the execution

    Each object is associated with a type

    the type defines the methods

    usually the type is set by the class of the object

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    13/39

    - 13 - Prof. Dr. H. Lichter

    Object Interfaces 1

    Each method defines a signature, consisting of

    a name,

    the argument objects

    a result object

    An interface of an object is defined by all his publicsignatures

    Method 1

    Method 1

    Method n

    arguments

    resu

    lts

    Signature

    Interface

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    14/39

    - 14 - Prof. Dr. H. Lichter

    Object Interfaces 2

    The interface of an object defines the offered services

    Server/Client-Relationship

    An object offers services which a client-object uses

    An object may be a client of one object and a server for anotherobject at the same time

    Sometimes offered services are implemented by using servicesof other (associated) objects

    ( Call-in/call-out interface)

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    15/39

    - 15 - Prof. Dr. H. Lichter

    Server/Client-Relationship

    Client Server

    Service

    Service

    uses

    Service

    Client

    Server

    Associated

    Object

    uses

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    16/39

    - 16 - Prof. Dr. H. Lichter

    Methods

    moveTo (p: Point)center : Point

    area : Integer

    Data

    origin = (10, 10)

    corner = (100, 400)

    E.g. Objects

    a rectangle

    a d o t

    Methods

    + (p : Point) : Point

    putX (i : Integer)

    putY (i : Integer)

    Data

    x= 10

    y = 10

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    17/39

    - 17 - Prof. Dr. H. Lichter

    E.g. Objects in Eiffel

    class Root-- declaration of objects

    featureb : Boolean; -- a basic typei : Integer -- a basic typer : Rectangle; -- a classp, q : Point; -- a cass

    Create isdo

    p.Create; -- create a dot pp.putX (10); -- send p a messagep.putY (10);r.Create; -- create a rectangle r

    ...

    b := r.contains (p);r.moveTo(p);

    p.Forget -- destroy dot p

    end;

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    18/39

    - 18 - Prof. Dr. H. Lichter

    receiver message name parameter

    r.contains (p)

    Messages

    Objects communicate by sending and receivingmessages

    Objects react to messages by executing a method

    The receiving object is responsible for

    understanding the message

    execution of the associated method (and its results)

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    19/39

    Classes

    Inheritance

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    20/39

    - 20 - Prof. Dr. H. Lichter

    Classes in a Domain Perspective

    Different things may have a common behavior

    my account, your account

    These similarities are expressed by a term orconcept

    Concepts can be generalized orspecialized we gethierarchies

    Concepts (or terms) make up the language of a problem domain

    giro account

    a c c o u n t

    fixed deposit

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    21/39

    - 21 - Prof. Dr. H. Lichter

    Classes in a Programming Perspective

    Concepts (or terms) are implemented through classes

    A class is characterized by a name

    an inheritance-relationship to superclasses

    data (attributes, variables)

    methods (operations, routines),

    methods can be public or private (ie for internal use only)

    Only the name and the public methods are visible

    From a class an unlimited number of objects can bederived (instantiated)

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    22/39

    - 22 - Prof. Dr. H. Lichter

    From Classes to Objects

    Classes

    A class defines how their instances will be created (andinitialized)

    Classes have a behavior for creating object

    Objects of a class

    perform the same set of methods

    have different states

    Basically

    a class is an Abstract Data Type

    an object is an Data Capsule

    Object oriented concepts extend the principle of InformationHiding

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    23/39

    - 23 - Prof. Dr. H. Lichter

    Inheritance 1

    Inheritance

    A basic feature of object oriented languages not found in otherparadigms

    Inheritance allows hierarchical class structures

    Technical implications

    All aspects of a superclass are valid for its subclasses also

    the unit of inheritance is the class Subclasses may specialize orextend aspects of their superclass

    The superclasses relationships are statically defined, theserelationships are typically unchanged during runtime.

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    24/39

    - 24 - Prof. Dr. H. Lichter

    Common features of different classes

    are collected in a own superclass

    are now inher i t ed from this superclass

    Rule

    Class A inherits from class B if and only if A is a specialization ofB

    Rectangle Circle

    Geo_Object

    Inheritance 2

    E.g.

    both have

    common aspects,

    E.g. area, extent

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    25/39

    - 25 - Prof. Dr. H. Lichter

    Types of Inheritance

    means ofmodification

    no of super-classes one / none unlimited

    extension /

    definition

    extension /redefinition /

    definition

    strictly singleinheritance

    non-strictly singleinheritance

    strictly multiple

    inheritance

    non-strictly multiple

    inheritance

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    26/39

    - 26 - Prof. Dr. H. Lichter

    Examples 1

    class Rectangle

    inherit

    Geo_Object

    define moveTo, contains

    feature

    origin, corner : Point;...

    end -- class Rectangle

    class Geo_Object

    featuremoveTo: (p : Point) is

    deferredend;

    contains (p : Point) : Boolean isdeferredend;

    end -- class Geo_Object

    class Circle

    inherit

    Geo_Object

    define moveTo, contains

    feature

    center : Point;

    radius : Integer:

    ...

    end -- class Circle

    Abstract superclass

    for specification

    Simple inheritance

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    27/39

    - 27 - Prof. Dr. H. Lichter

    class Rectangle

    inheritGeo_Object

    define moveTo, contains

    featureorigin, corner : Point;...

    end -- class Rectangle

    class DisplayableRectangle

    inheritRectangle

    inheritDispayableObject

    define psDescription

    feature...

    end -- classDisplayableRectangle

    class DisplayableObject

    featurepsDescription : String is

    deferredend;

    end -- class DisplayableObject

    class Geo_Object

    featuremoveTo: (p : Point) isdeferredend;

    contains (p : Point) : Boolean isdeferredend;

    end -- class Geo_Object

    Examples 2

    Multiple inheritance

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    28/39

    - 28 - Prof. Dr. H. Lichter

    Examples 3

    giro account

    deposit

    paytransferinterest

    0815 0816

    a c c o u n t

    fixed depositcurrency

    beginend

    personal drawing creditcredit card

    transfer

    4711 4712

    owner: Horst Lichternumber: 0815balance: $1000

    interest rare: 0,5 %personal drawing credit: $1000credit card: visa 3249-3458-...

    owner

    numberbalance

    interest rate

    Simple Inheritance

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    29/39

    - 29 - Prof. Dr. H. Lichter

    Examples 4

    depositpaytransferinterest

    0815 0816

    a c c o u n t

    fixed depositcurrencybeginend

    personal drawing creditcredit card

    transfer

    4711 4712

    ownernumberbalance

    interest rate

    Multiple Inheritance

    te lebanking O bject code log inchange code

    giro account

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    30/39

    - 30 - Prof. Dr. H. Lichter

    Abstract classes

    A class is not necessarily a complete implementation

    All methods ofconcrete classes are executable

    Some of the methods ofabstract (virtual) classes are not yetimplemented (only specified)

    Concrete classes

    can have unlimited instances (by receiving a create-message)

    Abstract classes

    must not have any instances

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    31/39

    - 31 - Prof. Dr. H. Lichter

    Abstract Classes Methods

    Abstract methods

    Abstract methods are not yet implemented This has to be done by the subclasses.

    Abstract methods define an interface for all subclasses

    If a method only implements a standard behavior, there is a slotfor the subclasses to extend this behavior (hook)

    Template methods

    Template methods implement a complete algorithm byabstractmethods

    It can only run when the concrete methods are implemented insubclasses

    Basic methods

    Basic methods are fully executable methods of a abstract class(i.e. completely implemented)

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    32/39

    - 32 - Prof. Dr. H. Lichter

    public String toString() {

    /* Returns a string representation of the object. */

    return getClass().getName()+'@'+integer.toHexString(hashCode());

    /* Standard implementation returns name of the class of which the

    object is an instance and the hexadecimal representation of thehash code */}

    public String toString() {return x + " , " + y ;

    }

    Example Hook Method

    Each object can receive the message toString() (asimplemented in the abstract class Object)

    Redefinition in the Class Point

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    33/39

    - 33 - Prof. Dr. H. Lichter

    Example - Template Method

    Each instance ofAbstractCollection (and istsubclasses) can receive the message clear()

    public void clear() {

    /* Removes all of the elements from this collection.This collection will be empty after this methodreturns unless it throws an exception. */

    Iterator iter = this.iterator();

    while (iter.hasNext()) {iter.next();iter.remove();

    }

    }

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    34/39

    - 34 - Prof. Dr. H. Lichter

    Superclasses vs. subclasses

    Inherited aspects can be modified by the subclass inthree ways:

    Extension

    add new aspects (data or methods)

    Redefinition overwrite existing methods

    Definition

    implement abstract methods

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    35/39

    - 35 - Prof. Dr. H. Lichter

    Example - Extension

    class Geo_Object

    featuremoveTo: (p : Point) is

    deferredend;

    contains (p : Point) : Booleanis

    deferred

    end;end -- class Geo_Object

    class Rectangle

    inheritGeo_Object

    define moveTo, contains

    featureorigin, corner : Point;

    feature

    height : Integer isdo ...end;

    width : Integer isdo ...end;

    ...end -- class Rectangle

    extending with

    additional aspectsheight and width

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    36/39

    - 36 - Prof. Dr. H. Lichter

    Example - Definition

    class Geo_Object

    featuremoveTo: (p : Point) is

    deferredend;

    contains (p : Point) : Booleanis

    deferred

    end;end -- class Geo_Object

    class Rectangle

    inheritGeo_Object

    define moveTo, contains

    featureorigin, corner : Point;

    featuremoveTo: (p : Point) is

    doorigin := origin + p;corner := corner + p;

    end;contains (p : Point) : Boolean

    isdo

    ...end;

    ...end -- class Rectangle

    definition of the

    proposed aspectsmoveTo and contains

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    37/39

    - 37 - Prof. Dr. H. Lichter

    Example - Redefinition

    class DisplayableObjectfeature

    psDescription: String isdeferredend;

    end -- class DisplayableObject

    class Rectangle...feature

    initialize is doorigin.Create; corner.Create;origin.initialize;corner.initialize;

    end...

    end -- class Rectangleclass DisplayableRectangle

    inheritRectanglerename initialize as initRectredefine initialize

    ...feature

    color : Color;

    featureinitialize is do

    current.initRect;color.create; color.black;

    end;...

    end -- class DisplayableRectangle

    the featureinitialize doesnt fit

    anymore so it must be

    redefined

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    38/39

    - 38 - Prof. Dr. H. Lichter

    Design Classes

    Relationships between classesuse

    inherit

    for specificationfor implementation

    Code(Static)Classes

    (partially) implemented abstract data types

    Run-time(Dynamic)

    ObjectsInstances of the classes

    Relationships between objects

    Send and receive messages

    Aspects of OO Architectures

    Classes are part of the

    design andthe code

  • 7/28/2019 SE 03 OO Concepts [Read-Only]

    39/39

    - 39 - Prof. Dr. H. Lichter

    Summary

    Central modeling concepts

    objects

    classes

    inheritance

    Classes model concepts of the domain

    Inheritance is used to model generalization /specialization

    This leads to abstract classes

    Subclasses can extent, redefine, define methods ofsuperclasses