Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming

Similar presentations


Presentation on theme: "Object-Oriented Programming"— Presentation transcript:

1 Object-Oriented Programming
Introduction What is an object Collaboration Among Objects Classes Relationships Between Classes “Use”, “Has”, “Is a” Vocabulary

2 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 ...

3 ... 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 q particle charge c speed of light e charge of the electron me rest mass of the electron n electron density of the target I mean excitation potential of the target ... Particle properties Physical constants Material properties

4 ... 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 We can now let the particle to execute CalculateDeDx() using material and constant parameters: particle.CalculateDeDx(material, constants) speed e density speed of light energy charge of e Ion. potential charge mass of e distance Calculate DeDX() Particle Material Constants We have intuitively introduced 3 objects in order to describe the “Bethe-Bloch formula”

5 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 State (variables) variable variable Behaviour (methods) variable An object is an entity comprised of variables and methods

6 What is an object ? Examples of objects: Particle Material speed
CalculateDeDx speed e density energy Move charge Ion. potential distance ... Particle Material

7 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

8 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 CalculateDeDx CalculateDeDx CalculateDeDx 2212 2112 PDG PDG PDG Move charge Move 1 Move charge charge energy energy energy 10 keV 15 keV Proton (An instance) Neutron (An instance) The Particle class

9 Classes – UML notation UML (Unified modelling language) = a graphical language for modelling data and their treatment Particle PDG charge energy CalculateDeDx() Move() Class name CalculateDeDx PDG Data members or attributes Move charge energy Function members or methods The Particle class 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

10 Encapsulation and Information Hiding ...
We differentiate the external from the internal – the interface from the implementation We hide the inside from the clients Don't need to understand the methods and internal variables Unaffected by their nature Unaffected by their changes There is a "wall" around an object No other object can become dependent on internal state and services Public services (Interface) variable variable variable Implementation State (variables) Internal services (not-accessible from outside)

11 Polymorphism ... A class (A) can inherit the state (variables) and behaviour (methods) of another class (B) The relationship "Is a" ( "Inherits from") 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 Hadron Boson

12 ... Polymorphism 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 super-class and provide their specialized implementation Eg. Bethe-Bloch formula is not suitable for calculating DeDx for electrons, we may need to redefine CalculateDeDx() Polymorphism An ability to create an object that has more than one form The objects belonging to different types can respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run-time (this is called late binding or dynamic binding). When processing a particle of electron type, its special implementation of CalculateDeDx() will be called

13 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 Polymorphism = ability to handle different objects in a common manner


Download ppt "Object-Oriented Programming"

Similar presentations


Ads by Google