Você está na página 1de 32

Introduction to Object-Oriented Programming

Objects and classes Encapsulation and information hiding Mental exercises


Classification and exemplification ggregation and decomposition !enerali"ation and speciali"ation

Inheritance Pol#morphism and d#namic binding $a%a an example of an object-oriented programming language

Program example &istor# of $a%a Comparison to C'C(

OOP: Introduction

Objects and Classes


Mammal *+o-legs ,er# large brains Omni%orous -plants ( meat.

Mammal *us/s 0our legs &erbi%orous -plant eater.

OOP: Introduction

*he Object Concept



n object is an encapsulation of data2 n object has


identit# -a uni3ue reference.

social securit# number -cpr.4 emplo#ee number4 passport number hungr#4 sad4 drun/4 running4 ali%e eat4 drin/4 +a%e4 smile4 /iss

state4 also called characteristics -%ariables.

beha%ior -methods.

n object is an instance of an class2


class is often called an Abstract Data Type (ADT)2

OOP: Introduction

*he Class Concept



class is a collection of objects -or values. and a corresponding set of methods. class encapsulates the data representation and ma/es data access possible at a higher le%el of abstraction2 set of %ehicles +ith operations for starting4 stopping4 dri%ing4 get /m'liter4 etc2 Example ): time inter%al4 start time4 end time4 duration4 o%erlapping inter%als4 etc2 Example 1: string4 upper case4 compare4 lo+er case4 etc2

Example 1:

str.equals(otherStr) 6 class'$a%a st#le strcmp(str, otherStr) 6 C st#le

OOP: Introduction

Encapsulation and Information &iding


8ata can be encapsulated such that it is in%isible to the
9outside +orld:2 8ata can onl# be accessed %ia methods2
8ata 0unction 0unction 0unction Procedural
OOP: Introduction

send message

8ata Method Method Method Class

Encapsulation and Information &iding4 cont2


<hat the 9outside +orld: cannot see it cannot depend on= *he object is a 9fire-+all: bet+een the object and the 9outside
+orld:2 *he hidden data and methods can be changed +ithout affecting the 9outside +orld:2
Client interface

Outside +orld

n object

,isible data and methods

&idden -or encapsulated. data and methods


OOP: Introduction ;

Class %s2 Object


Class description of the common properties of a set of objects2 concept2 class is a part of a program2 Object representation of the properties of a single instance2 phenomenon2 n object is part of data and a program execution2

Example 1: Person Example ): lbum

Example 1: ?ill Clinton4

?ono4 ,iggo $ensen2 Example ): &ard 8a#@s Aight4 $oshua *ree4 Bic/ie Cee $ones2
>

OOP: Introduction

Connection bet+een Object and Class


In object-oriented programming +e +rite classes

*he text files +e create contain classes= Etatic 9One: class contains a 9receipe: on ho+ to ma/e objects 8#namic 9Man#:
Ingrediens )7F g digesti%e biscuits food processor 1)7 g soft bro+n sugar saucepan 1)7 g butter +ooden spoon 7F g raisins 1D cm sand+ich tin -greased. 1 tablespoons cocoa po+der fridge 1 egg4 beaten /nife )7 g G 1 o" )27 cm G 1 inch Process blend ba/e

Objects are created from classes


OOP: Introduction source http:''+++2icbl2h+2ac2u/'ltdi'coo/boo/'chocolateHca/e'

source http:''+++2filflora2com

*#pe and Interface

n object has t#pe and an interface2


Account balance() withdraw() deposit() *#pe Interface

*o get an object *o send a message

Account a = new Account() Account b = new Account() a.withdraw() b.deposit() a.balance()


I

OOP: Introduction

Instantiating Classes

n instantiation is a mechanism +here objects are created from a class2 l+a#s in%ol%es storage allocation for the object2 mechanism +here objects are gi%en an initial state2 Dynamic Instantiating In the method part of a program2 d#namic instance is created explicitl# +ith a special command2

Static Instantiating In the declaration part of a program2 static instance is implicitl# created

OOP: Introduction

1F

Interaction bet+een Objects


Interaction bet+een objects happens b# messages being send2 message acti%ates a method on the calling object2
n object O1 interacts +ith another object O) b# calling a method on O) -must be part of the client interface.2

9O1 sends O) a message:

O1 and O) must be related to communicate2 *he call of a method corresponds to a function -or procedure.
call in a non-object-oriented language such as C or Pascal2
O1
message
OOP: Introduction

message

O)
message
11

O1

Phenomenon and Concept



phenomenon is a thing in the 9real: +orld that has indi%idual existence2

an object

concept is a generali"ation4 deri%ed from a set of phenomena and based on the common properties of these phenomena2

a class

Characteristics of a concept

name Intension4 the set of properties of the phenomenon E tension4 the set of phenomena co%ered b# the concept2

OOP: Introduction

1)

Classification and Exemplification4 Examples


hat4 )14 154 mouse4 telephone4 boo/4 ID4 572154 hello

numbers: +ords:

)14 154 ID4 57215 hat4 mouse4 telephone4 boo/4 hello

mouse4 t#rannosaurus rex4 allosaurus4 elephant4 %elociraptor


dinosaur: mammal:

t#rannosaurus rex4 allosaurus4 %elociraptor mouse4 elephant

OOP: Introduction

11

Classification and Exemplification4 cont2



classification is a description of +hich phenomena that belongs to a concept2 n e emplification is a phenomenon that co%ers the concept

Concept
classification exemplification

Phenomenon

OOP: Introduction

15

ggregation and 8ecomposition4 Example


Idea: ma/e ne+ objects b# combining existing objects2 !eusing the implementation"
Engine start() stop() Gearbox up() down() Door open() close() existing classes ggregation ar Engine Gearbox Doors!"# start() dri$e() ne+ class

ar 9has-a: Gearbox and ar 9has-an: Engine


OOP: Introduction 17

ggregation and 8ecomposition



n aggregation consists of a number of -sub-.concepts +hich collecti%el# is considered a ne+ concept2 decomposition splits a single concept into a number of -sub-.concepts2

Concept
decomposition

Concept Concept Concept


aggregation

Concept Concept Concept

Concept

OOP: Introduction

1;

!enerali"ation and Epeciali"ation

OOP: Introduction

source : +++2geolog#2ucda%is2edu' J!EC1)'dinosauria2&tml

1>

!enerali"ation and Epeciali"ation4 cont2


#enerali$ation creates a concept +ith a broader scope2 Speciali$ation creates a concept +ith a narro+er scope2 !eusing the interface"
Concept
speciali$ation

Concept C
generali$ation

Concept ? ,ehicle Car &atchbac/


OOP: Introduction

Concept 8

*ruc/ Eedan Pic/up

Etation car

1D

!enerali"ation and Epeciali"ation4 Example


Inheritance: get the interface from the general class2 Objects related b# inheritance are all of the same t#pe2
Shape draw() resi%e() ircle draw() resi%e() &ine draw() resi%e() 'ectangle draw() resi%e() Square draw() resi%e()

OOP: Introduction

Square 9is-a: Shape or Square 9is-li/e-a: Shape

1I

!enerali"ation and Epeciali"ation in $a%a


(b)ect clone() equals() toString() ... Shape draw() resi%e() ircle &ine 'ectangle

OOP: Introduction

)F

Pol#morphism and 8#namic ?inding


$oid doSomething(Shape s)* s.draw()+ ,, -magicall./ calls the speci0ic class s.resi%e()+ 1 ircle c = new ircle()+ &ine l = new &ine()+ 'ectangle r = new 'ectangle()+ doSomething(c)+ doSomething(l)+ doSomething(r)+ ,, d.namic binding

%olymorphism: One piece of code +or/s +ith all shape


objects2 Dynamic binding: &o+ pol#morphism is implemented2

OOP: Introduction

)1

?enefit !enerali"ation and Epeciali"ation


*a/e pre%ious Ehape class hierarch#

remo%e inheritance remo%e general and abstract class Shape

'ectangle draw() resi%e() ircle draw() resi%e()

Square draw() resi%e() &ine draw() resi%e()

OOP: Introduction

))

Code Example4 Be%isited


$oid doSomething( ircle c)* c.draw()+ c.resi%e()+ 1 $oid doSomething(&ine l)* l.draw()+ l.resi%e()+ 1 ircle c = new ircle()+ &ine l = new &ine()+ 'ectangle r = new 'ectangle()+ doSomething(c)+ doSomething(l)+ doSomething(r)+
OOP: Introduction

$oid doSomething('ectangle r)* r.draw()+ r.resi%e()+ 1 $oid doSomething(Square s)* s.draw()+ s.resi%e()+ 1

Eimilar code is repeated


)1

$a%a Program Etructure


,, comment on the class public class 2.3rog * String s = /4iggo/+ ,55 5 6he main method (comment on method) 5, public static $oid main (String!# args)* ,, )ust write some stu00 S.stem.out.println (78ello 9orld7)+ 1 1

%ariable method header

method bod#

OOP: Introduction

)5

$a%a Class Example ar


,55 A simple class modeling a car. 5, public class ar * ,, instance $ariables pri$ate String ma:e+ pri$ate String model+ pri$ate double price+ ,, constructor public ar(String m, String mo, double p) * ma:e = m+ model = mo+ price = p+ 1 ,, string representation o0 the car public String toString() * return 7ma:e; 7 < ma:e < 7 model; 7 < model < 7 price; 7 < price+ 1 1
OOP: Introduction )7

?#te Code %s2 Executable


2.3rog.)a$a )a$ac 2.3rog.)a$a gcc 2.3rog.cpp =o m.prog.exe $a%a Class 0ile 2.3rog.class Portable ?#te Code $a%a ,irtual Machine Operating E#stem $a%a'CK +orld
OOP: Introduction

2.3rog.cpp

Executable m#prog2exe Operating E#stem C(( +orld


);

&istor# of $a%a
1IIF Oa/ -interacti%e tele%ision4 big failure. 1II5 $a%a -for the Internet.

Main feature: L<rite Once4 Bun n# <hereL GM +rap the operating s#stem so the# all loo/ the same fresh start -no bac/+ard compatibilit#. 9Pure: OOP: C(( E#ntax4 Emalltal/ st#le Impro%ements o%er C(( much harder to +rite a bad program Internet programming

8esigned for

,er# hard to create a %irus Bun in a +eb bro+ser -and at the ser%er.

*here is a speed issue -from $a%a 121 and up much better. Canguage %er# similar to $a%a Commen-Canguage Buntime -CCB. supports 1F( languages
)>

CK Microsoft@s 9$a%a-Niller: project release )FF1


OOP: Introduction

8ifference from C'C((


E%er#thing resides in a class

%ariables and methods b#e b#e malloc()4 0ree()4 and si%eo0()

!arbage collection

Error and exception handling handling Ao global %ariables or methods Ao local static %ariables Ao separation of declaration and implementation

?#e b#e header files

Ao explicit pointer operations -uses references. Ao preprocessor -but something similar. &as fe+er 9dar/ corners: &as a much larger standard librar# -$a%a 8e%eloper Nit or $8N. OOP: Introduction

)D

Eummar#
Classes are 9recipes: for creating objects ll objects are instances of classes Encapsulation

Ne# feature of object-oriented programming Eeparation of interface from implementation It is not possible to access the hidden'encapsulated parts of an object

ggregation and decomposition


9has-a: relationship 9is-a: or 9is-li/e-a: relationship Eoftening static t#ping

!enerali"ation and speciali"ation -inheritance. Pol#morpishm'd#namic binding

OOP: Introduction

)I

Common Mista/es and Errors


,, what is ugl. here> public class main * public static $oid main(String!# args)* S.stem.out.println(-8ello 9orld/)+1 1 ,, what is wrong here> public class 2. lass * public $oid static main(string!# args)* s.stem.out.println(-8ello 9orld/)+1 1 ,, what is ugl. here> public class 2. lass * public static $oid main(String!# args)* S.stem.out.println(-8ello 9orld/)+1 1+
OOP: Introduction 1F

Etructuring b# Program or 8ataO


<hat are the actions of the program %s2 +hich data does the
program act on2 Top&do'n( Etep+ise program refinement )ottom&up: 0ocus on the stable data parts then add methods

Object-oriented programming is bottom-up2 Programs are


structure +ith outset in the data2

C and Pascal programs are t#picall# implemented in a more top-do+n fashion2

OOP: Introduction

11

Pure Object-Oriented Canguages


0i%e rules Psource: lan Na#Q

E%er#thing in an object2 program is a set of objects telling each other +hat to do b#


sending messages2 Each object has its o+n memor# -made up b# other objects.2 E%er# object has a t#pe2 ll objects of a specific t#pe can recei%e the same messages2

$a%a brea/s some of these rules in the name of efficienc#2

OOP: Introduction

1)

Você também pode gostar