You are on page 1of 39

History Meta-model of Objects Objects & Messages Classes Inheritance

Background Meta-Model

History of Object Orientation


Simula: Ole-Johan Dahl and Krysten Nygaard, Norway, 1967
A language for simulation (i.e. modeling)

Prof. Dr. H. Lichter

- 3-

Why objects?
Finally I would like to try to answer the following fair question: How could it happen that a team of two working in the periphery of Europe could hit on programming principles of lasting importance? No doubt a bit of good luck was involved. We were designing a language for simulation modeling, and such models are most easily conceived of in terms of cooperating objects. Our approach, however, was general enough to be applicable to many aspects of system development.
Ole-Johan Dahl: sdm Conference Software Pioneers, Bonn, 2001

Prof. Dr. H. Lichter

- 4-

OO Languages - Milestones

Prof. Dr. H. Lichter

- 5-

Aspects of Object Oriented Languages


Simula Smalltalk-80 Eiffel C++ Java Modula-3

Ada Modula-2

CLU (Ada) (Modula-2)

Object based Objects


Objects are encapsulated data, described one by one Classes describe types of objects Data, objects can be derived from classes.
Prof. Dr. H. Lichter

Class based Objects Classes

Object oriented Objects Classes Inheritance

Inheritance defines a specializationhierarchy between classes


- 6-

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 for modeling are defined Different programming languages may define different metamodels
Prof. Dr. H. Lichter

- 7-

A Metamodel of Objects 2
inherits uses

+Sub +Super
0..* 0..* 0..*

+Server +Client
0..*

redefines

Program Module

implements 0..* Abstract Method 1..* specifies Abstract Class

Abstract Class Method

Abstract Object M ethod sends

Class

defines / redefines

is_instance_of

Method
0..*

triggers execution of

Message
receives

Object

Class Method

Object Method
1..*

accesses 0..*

Attribute

0..*

1..* accesses 0..*

Class Attribute

Object Attribute

Prof. Dr. H. Lichter

- 8-

Objects Messages

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 and how 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

Prof. Dr. H. Lichter

- 10 -

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

Prof. Dr. H. Lichter

- 11 -

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 will be 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

Prof. Dr. H. Lichter

- 12 -

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 public signatures

Signature
results

arguments

Method 1 Method 1 Method n

Interface

Prof. Dr. H. Lichter

- 13 -

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 another object at the same time Sometimes offered services are implemented by using services of other (associated) objects
( Call-in/call-out interface)

Prof. Dr. H. Lichter

- 14 -

Server/Client-Relationship
Server uses Service Service Associated Object Client uses Server Service

Client

Prof. Dr. H. Lichter

- 15 -

E.g. Objects
a rectangle Data origin = (10, 10) corner = (100, 400) Methods moveTo (p: Point) center : Point area : Integer Data x= 10 y = 10 Methods + (p : Point) : Point putX (i : Integer) putY (i : Integer) a dot

Prof. Dr. H. Lichter

- 16 -

E.g. Objects in Eiffel


class Root -- declaration of objects feature b : Boolean; -- a i : Integer -- a r : Rectangle; -- a p, q : Point; -- a

basic type basic type class cass

Create is do p.Create; -- create a dot p p.putX (10); -- send p a message p.putY (10); r.Create; -- create a rectangle r ... b := r.contains (p); r.moveTo(p); p.Forget -- destroy dot p

end;

Prof. Dr. H. Lichter

- 17 -

Messages
Objects communicate by sending and receiving messages 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)

receiver

message name

parameter

r.contains (p)

Prof. Dr. H. Lichter

- 18 -

Classes Inheritance

Classes in a Domain Perspective


Different things may have a common behavior
my account, your account

These similarities are expressed by a term or concept


Concepts can be generalized or specialized we get hierarchies Concepts (or terms) make up the language of a problem domain
account

giro account

fixed deposit

Prof. Dr. H. Lichter

- 20 -

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 be derived (instantiated)

Prof. Dr. H. Lichter

- 21 -

From Classes to Objects


Classes
A class defines how their instances will be created (and initialized) 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 Information Hiding

Prof. Dr. H. Lichter

- 22 -

Inheritance 1
Inheritance
A basic feature of object oriented languages not found in other paradigms 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 or extend aspects of their superclass The superclasses relationships are statically defined, these relationships are typically unchanged during runtime.

Prof. Dr. H. Lichter

- 23 -

Inheritance 2
Common features of different classes
are collected in a own superclass are now inherited from this superclass

Rule
Class A inherits from class B if and only if A is a specialization of B
Geo_Object
both have common aspects, E.g. area, extent

E.g.

Rectangle

Circle

Prof. Dr. H. Lichter

- 24 -

Types of Inheritance
no of superclasses means of modification

one / none

unlimited

extension / definition

strictly single inheritance

strictly multiple inheritance

extension / redefinition / definition

non-strictly single inheritance

non-strictly multiple inheritance

Prof. Dr. H. Lichter

- 25 -

Examples 1

class Circle inherit Geo_Object define moveTo, contains

feature center : Point; class Geo_Object radius : Integer: feature moveTo: (p : Point) is deferred end; contains (p : Point) : Boolean is deferred end; end -- class Geo_Object ... end -- class Circle class Rectangle inherit Geo_Object define moveTo, contains

Abstract superclass for specification Simple inheritance

feature origin, corner : Point; ... end -- class Rectangle

Prof. Dr. H. Lichter

- 26 -

Examples 2
class Geo_Object feature moveTo: (p : Point) is deferred end; contains (p : Point) : Boolean is deferred end; end -- class Geo_Object

class Rectangle inherit Geo_Object define moveTo, contains feature origin, corner : Point; ... end -- class Rectangle

class DisplayableObject feature psDescription : String is deferred end; end -- class DisplayableObject

class DisplayableRectangle inherit Rectangle inherit DispayableObject define psDescription feature ... end -- class DisplayableRectangle

Multiple inheritance

Prof. Dr. H. Lichter

- 27 -

Examples 3
Simple Inheritance
owner number balance interest rate

account

deposit pay transfer interest

personal drawing credit credit card

giro account

currency begin end transfer

fixed deposit

owner: Horst Lichter number: 0815 balance: $1000 interest rare: 0,5 % personal drawing credit: $1000 credit card: visa 3249-3458-...

0815

0816

4711

4712
- 28 -

Prof. Dr. H. Lichter

Examples 4
code

telebanking Object

log in change code

owner number balance interest rate

account

deposit pay transfer interest

personal drawing credit credit card

giro account

currency begin end transfer

fixed deposit

Multiple Inheritance

0815
Prof. Dr. H. Lichter

0816

4711

4712
- 29 -

Abstract classes
A class is not necessarily a complete implementation
All methods of concrete classes are executable Some of the methods of abstract (virtual) classes are not yet implemented (only specified)

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

Abstract classes
must not have any instances

Prof. Dr. H. Lichter

- 30 -

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 slot for the subclasses to extend this behavior (hook)

Template methods
Template methods implement a complete algorithm by abstract methods
It can only run when the concrete methods are implemented in subclasses

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

Prof. Dr. H. Lichter

- 31 -

Example Hook Method


Each object can receive the message toString() (as implemented in the abstract class Object)
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 the hash code */ }

Redefinition in the Class Point


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

Prof. Dr. H. Lichter

- 32 -

Example - Template Method


Each instance of AbstractCollection (and ist subclasses) can receive the message clear()

public void clear() { /* Removes all of the elements from this collection. This collection will be empty after this method returns unless it throws an exception. */ Iterator iter = this.iterator(); while (iter.hasNext()) { iter.next(); iter.remove(); } }

Prof. Dr. H. Lichter

- 33 -

Superclasses vs. subclasses


Inherited aspects can be modified by the subclass in three ways:

Extension
add new aspects (data or methods)

Redefinition
overwrite existing methods

Definition
implement abstract methods

Prof. Dr. H. Lichter

- 34 -

Example - Extension
class Geo_Object feature moveTo: (p : Point) is deferred end; contains (p : Point) : Boolean is deferred end; end -- class Geo_Object
class Rectangle inherit Geo_Object define moveTo, contains feature origin, corner : Point; feature height : Integer is do ... end; width : Integer is do ... end; ... end -- class Rectangle

extending with additional aspects height and width

Prof. Dr. H. Lichter

- 35 -

Example - Definition
class Geo_Object feature moveTo: (p : Point) is deferred end; contains (p : Point) : Boolean is deferred end; end -- class Geo_Object

class Rectangle inherit Geo_Object define moveTo, contains feature origin, corner : Point; feature moveTo: (p : Point) is do origin := origin + p; corner := corner + p; end; contains (p : Point) : Boolean is do ... end; ... end -- class Rectangle

definition of the proposed aspects moveTo and contains

Prof. Dr. H. Lichter

- 36 -

Example - Redefinition
class DisplayableObject feature psDescription: String is deferred end; end -- class DisplayableObject
class Rectangle ... feature initialize is do origin.Create; corner.Create; origin.initialize; corner.initialize; end ... end -- class Rectangle

class DisplayableRectangle inherit Rectangle rename initialize as initRect redefine initialize ... feature color : Color; feature initialize is do current.initRect; color.create; color.black; end; ... end -- class DisplayableRectangle

the feature initialize doesnt fit anymore so it must be redefined

Prof. Dr. H. Lichter

- 37 -

Aspects of OO Architectures
Design
Classes for specification for implementation Relationships between classes use inherit

Code
(Static)

Classes (partially) implemented abstract data types

Classes are part of the design and the code

Run-time
(Dynamic)

Objects Instances of the classes Relationships between objects Send and receive messages

Prof. Dr. H. Lichter

- 38 -

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 of superclasses

Prof. Dr. H. Lichter

- 39 -

You might also like