# OOP - IPN OOP - IPN

Object-Oriented Programming

Introduction

What is an object

Collaboration Among Objects

Classes

Relationships Between Classes

– “Use”, “Has”, “Is a”

Vocabulary

1

Introduction ...

Let's try for example to see how to implement Bethe-Bloch

formula

where

– β = v / c

– v velocity of the particle

– E energy of the particle

– x distance travelled by the particle

– c speed of light

– ze particle charge

– e charge of the electron

– me rest mass of the electron

– n electron density of the target

– I mean excitation potential of the target

– ...

2

... Introduction ...

In traditional (algorithmic) programming, we would implement a

function that uses all parameters

– CalculateDeDx(v, E, x, c, q, e, me, n, I, ...)

– This would introduce in our program 10 (or more) independent

variables which we would have to handle (initialize, update etc.)

In Object-Oriented programming, we first identify the entities

with properties which belong together:

– v velocity of the particle

– E energy of the particle

– x distance travelled by the particle

Particle properties

– q particle charge

– c speed of light

– e charge of the electron

– me rest mass of the electron

Physical constants

– n electron density of the target

– I mean excitation potential of the target

– ...

Material properties

3

... Introduction

When we group the properties in the entities, we can simplify the

CalculateDeDx() function:

– CalculateDeDx(particle, material, constants)

We have introduced 3 objects to hold the related variables

speed

energy

charge

distance

Calculate

DeDX()

Particle

e density

Ion. potential

Material

speed of light

charge of e

mass of e

Constants

We can now let the particle to execute CalculateDeDx() using

material and constant parameters:

– particle.CalculateDeDx(material, constants)

We have intuitively introduced 3 objects

in order to describe the “Bethe-Bloch formula”

4

What is an object ?

“Something you can do things to” (G. Booch)

An object = an entity which has its state and behaviour

– The object retains its state within one or more variables

Variable = data appointed by an identifier

– The object implements its behaviour by the methods

Method = function associated with an object

Behaviour

(methods)

variable

variable

variable

State

(variables)

An object is an entity comprised of variables and methods

5

What is an object ?

Examples of objects:

Move

CalculateDeDx

speed

energy

charge

distance

e density

Ion. potential

...

Particle

Material

6

Collaboration Among Objects

The object appears as a component of a program that contains

many other objects

It is through the interaction of its objects that the program

operates at a higher level of functionality and complexity

The objects interact with each other by sending messages

The message includes:

– The message receiver to whom

the message is sent (Particle)

– The name of the method to be

executed (CalculateDeDx)

– The parameter (or parameters)

that the method requires

(material, constants)

CalculateDeDx(material, constants)

CalculateDeDx

You

Particle

7

Classes

In the real world we have a number of objects of the same type:

– The proton and neutron are two of many particles in the world

– Speaking OO (Object Oriented) - we say that the proton is an instance

of the class of objects known as particles

A class is a prototype that declares properties common to a set of

objects (variables and methods)

– It also appears as a mold or a prototype from which it is possible to

create objects

An instance is a case, a realization of an object

Move

CalculateDeDx

PDG

charge

energy

Move

CalculateDeDx

PDG

charge

energy

2212

1

10 keV

Move

CalculateDeDx

PDG

charge

energy

2112

0

15 keV

The Particle class

Proton

(An instance)

Neutron

(An instance)

8

Classes – UML notation

UML (Unified modelling language) = a graphical language for

modelling data and their treatment

CalculateDeDx

Particle

Class name

Move

PDG

charge

energy

PDG

charge

energy

Data members or

attributes

The Particle class

CalculateDeDx()

Move()

Function members

or methods

If the details of the class are not

important in the context, the class can

be well represented by a simple

rectangle

Particle

Class name

9

Encapsulation and Information

Hiding ...

We differentiate the external from the internal – the interface

from the implementation

Public services

(Interface)

Implementation

State

(variables)

variable

variable

variable

Internal services

(not-accessible from outside)

10

... Encapsulation and Information

Hiding

An object encapsulates knowing how to be itself

It knows how to deliver its services

It knows how to remember its state

– In a services offered wrapper

Having differentiated the outside from the inside, we hide the

inside from the clients

– Don't need to understand the mechanisms and variables

– Not impacted by the nature of the mechanisms and variables

– Not impacted by changes of the mechanisms and variables

There is a “wall” around an object

– Its knowledge how to be itself is private

– Its mechanisms (code and variables) are hidden

– No other object can become dependent on those mechanisms

11

Relationships between Classes

"Has"

The program design has to define object entities described by the

classes and their relations

The relationship "Has"

– A class (A) may contain one or more types (B, C, ...)

– We say that class A is a composite of class B, C, ...

– Eg. our class Material may contain a variable which represents a

collection of chemical elements that compose the material

elements

Material

Element

12

Relationships between Classes

"Use"

The relationship "Use"

– A class (A) may need to use another class (B) but does not necessarily

contain it

– Typically class A sends messages to class B which is not contained as a

data member

– Eg. Our class Particle defines the method CalculateDeDx which uses as

a parameter the objects of type Material and Constants

CalculateDeDx(material, ...)

Material

Particle

You

13

Relationships between Classes

"Is a"

The relationship "Is a" ( "Inherits from")

– A class (A) can inherit the state (variables) and behaviour (methods) of

another class (B)

– We say class A is a base class (or a generalization, or a super-type) of

class B and class B is a class derived from Class A (or a specialization,

or a subtype)

– For example, leptons, hadrons, bosons are all particles, but belong to

different groups, each with their special properties

Particle

Lepton

Boson

14

Relationships between Classes

"Is a"

– Derived classes share the same states and the same methods, but they

can add their own data members and/or methods

Eg. depending on the particle subtype, they may have 2 quarks or 3 quarks

– Derived classes may also redefine the method inherited from its superclass

and provide their specialized implementation

Eg. Bethe-Bloch formula is not suitable for calculating DeDx for electrons,

we may need to redefine CalculateDeDx()

15

Vocabulary

Object = a computing entity comprised of variables and methods

Variable = data appointed by an identifier

Method = function associated with an object

Message = the means of interaction and communication among

objects

Class = a prototype that declares properties common to a set of

objects (variables and methods)

Instance = is a case, a realization of an object of a given class

Encapsulation = packaging of object variables behind its methods

"Has" = class A has class B if one of its variables is an instance of

class B

"Use" = class A uses class B if it needs an instance of class B in

one of its methods

"Is a" = derived class B inherits the state (variables) and

behaviour (methods) of base class, A

16

More magazines by this user
Similar magazines