Introduction to Object-oriented Software Development

Download Report

Transcript Introduction to Object-oriented Software Development

Introduction to
Object-oriented Software
Development
Introduction to Object-oriented Software Development, page no. 1, 7/18/2015
Object-orientation Is a
Modeling Technique
• We perceive the world as a world of objects.
You look around and you see a chair, a table, a
person, and the objects are related in one way or
another.
• The object-oriented technique tries to imitate the
way we think, a system developed by using the
object-oriented technique can be seen as a system
consisting of a number of objects which cooperate
to solve a task.
Introduction to Object-oriented Software Development, page no. 2, 7/18/2015
Objects
• You find objects of different
types in the real world.
• At this moment you have x
number of busses, one of them
is represented by the buss
picture.
• The picture of the bus is a
symbol and not the real thing.
• In UML you can use icons to
represent objects or:
use rectangles and specify
object name and object type.
myChair:
:Bus
Object name and type
Object name and no type
:Truck
:Plane
:Ship
PN62001 :Car
Anonymous object, only type is given
Introduction to Object-oriented Software Development, page no. 3, 7/18/2015
When You Classify Objects
You Decide Their Type
Furniture
myChair:
yourChair:
theBrownSofa:
The class of ”people objects”
People
The subclass of ”female objects”
Female
Male
Jane:
James:
Introduction to Object-oriented Software Development, page no. 4, 7/18/2015
There Are Typically Many
Ways to Classify Objects
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
xxx:
The classes overlap
• Jim might be classified as a person, a male person, an employee, a
student and so on, the context decides.
Introduction to Object-oriented Software Development, page no. 5, 7/18/2015
1-
Objects and Class
• By watching the objects we recognize that some objects share the
same type of qualities (a person has a name, the same number of
chromosomes, ... ) so we might formulate a general concept –
that is define a class. This process is called abstraction.
• Plato talked about the ideas behind, you have the idée horse and
you have concrete horses. For him, the ideas had their own
existence, this idée concept is very close to the class concept.
• When you program in an object-oriented language you can define
your own classes and make objects of the different classes.
(So is object-oriented programming a divine activity or what?)
Introduction to Object-oriented Software Development, page no. 6, 7/18/2015
2-
Objects and Class
• There are object-oriented languages were you do not have classes,
only objects (e.g. Self). This is more like the way Aristoteles
figured the world: the classes have no exsistens by they own, the
objects have an immanent form which makes it an object of a
certain class.
• The more common languages like C++ and Java lets you define
classes and than make objects of this classes.
• A class describes a set of objects that share the same attributes,
behaviour and relationships.
Introduction to Object-oriented Software Development, page no. 7, 7/18/2015
Objects in UML
”A concrete manifestation of an abstraction;
An entity with a welldefined boundary and
identity that encapsulates state and
behavior; An instance of a class.”
Introduction to Object-oriented Software Development, page no. 8, 7/18/2015
Making a Model
• You have a problem domain and you want to make a
system that solves some problem in this domain.
• You single out what is essential for your problem, and
you also typically simplify reality.
• You make a model of your system, objects from reality
are mapped directly into objects in the model.
chair:
Seated in
Tom:
car:
reality
mapping
model
owner
relation (link)
Introduction to Object-oriented Software Development, page no. 9, 7/18/2015
Class and Objects in UML
class
objects
Person
SSN
name
address
drive()
Run()
...
Class name
attributes
operations
Jane:Person
SSN = 123...
name= Jane
address=Norway
James:Person
SSN = 122...
name = James
address = USA
• You when you declare the class you select which attributes
and operations are of interest for your system.
• Objects of the given class will have individual values for the
specified attributes.
Introduction to Object-oriented Software Development, page no. 10, 7/18/2015
Class Hieararchy ~
Inheritance
• An employee is a special type of person, so if you already have
a Person class and need a Employee class then it should be
possible to take advantage of the already existing Person class.
superclass
Person
SSN
name
address
inheritance
or specialization
subclass
Employee
title
department
Jane:Employee
Employee is a
spesialization
of Person
SSN
= 123...
name
= Jane
address = Norway
title
= accountant
deparment = accounting
An object of type Employee is also a Pers
An will have all attributes that a Person
plus all of its own.
Introduction to Object-oriented Software Development, page no. 11, 7/18/2015
Multiple
Inheritance
• Some times when we describe a
new class we see that this new class
should have characteristics from
two (or more) existing classes.It is
then possible to inherit from both
classes.
• Not all object-oriented language
support multiple inheritance (e.g.
Java), some object-oriented
theoreticians claim that multiple
inheritance should not be used!
Person
name
Employee
title
Owner
WorkingOwner
Cassette
Book
volume : Integer
volume : Integer
CassetteBo
ok
Introduction to Object-oriented Software Development, page no. 12, 7/18/2015
One Common Ancestor for
All Classes
Object
getClass()
hashCode()
equals()
clone()
toString()
notify()
notifyAll()
wait()
wait()
wait()
finalize()
Object()
• In Java all classes have a common ancester
called Object, this is not the case in C++.
Person
Car
Employee
Introduction to Object-oriented Software Development, page no. 13, 7/18/2015
1 - Relationships
• Objects are typically connected in some way or another. For
example: Jane is married to James, Jane owns a car.
Jane:Person
SSN = 123...
name= Jane
address=Norway
James:Person
married
SSN = 122...
name = James
address = USA
owns
someWreck:Car
Introduction to Object-oriented Software Development, page no. 14, 7/18/2015
2 - Relationships
• Some objects can be seen as consisting of other
objects.
Head
1
1
Arm
2
1
Person
1
2
Leg
Introduction to Object-oriented Software Development, page no. 15, 7/18/2015
Objects Have Behaviour
CoffeeMachine
Person
pourCoffe()
addMilk()
addOneLumpOfSugger()
Jane : Person
operations ~ behaviour
office : CoffeeMachine
sending a message
pourCoffe( )
addOneLumpOfSugger( )
time
addOneLumpOfSugger( )
addMilk( )
Introduction to Object-oriented Software Development, page no. 16, 7/18/2015
An Object-oriented
Program
• Objects work together and solves problems.
• Objects have state, the values of the attributes defines
the state. The state change when the attribute values
changes.
• Some objects are linked together.
• The objects communicate by sending messages to each
other (messages follows the links). Sending a message is the same
as asking an object to perform one of it’s operations.
Introduction to Object-oriented Software Development, page no. 17, 7/18/2015
1 - Paradigms
of
programming
• Imperative programming (FORTRAN, C):
• Algorithm abstraction (give name to a sequence of
instructions)
• Focus on the algorithms
• Data in separate structures
• Stepwise refinement (structured programming)
• Datamodelling (COBOL, SQL):
• Focus on data an the static relationships
• Data abstractions, give name to a group of data (records)
Introduction to Object-oriented Software Development, page no. 18, 7/18/2015
2 - Paradigms
of
programming
• Object-oriented programming (Smalltalk, C++, Java):
• Data and behaviour is put together
• More mathematical approaches:
• Logical programming (Prolog)
• Functional programming (Lisp)
Introduction to Object-oriented Software Development, page no. 19, 7/18/2015
The First Object-oriented
Language: Simula
• Introduced in 1966 (or earlier).
• Created by Ole-Johan Dahl and Kristen Nygaard.
• A language for simulation but became a general purpose
programming language.
Allan Key: “Simula is a major improvement
of most of its sucessors”
Introduction to Object-oriented Software Development, page no. 20, 7/18/2015