object-oriented principals dwight deugo nesa matic

27
Object-Oriented Object-Oriented Principals Principals Dwight Deugo ([email protected]) Dwight Deugo ([email protected]) Nesa Matic ([email protected]) Nesa Matic ([email protected]) www.espirity.com

Upload: maria-coleen-horton

Post on 17-Jan-2018

221 views

Category:

Documents


0 download

DESCRIPTION

3 © , Espirity Inc. Module Overview 1.Object-Oriented Principals

TRANSCRIPT

Page 1: Object-Oriented Principals Dwight Deugo Nesa Matic

Object-Oriented PrincipalsObject-Oriented PrincipalsDwight Deugo ([email protected])Dwight Deugo ([email protected])Nesa Matic ([email protected])Nesa Matic ([email protected])

www.espirity.com

Page 2: Object-Oriented Principals Dwight Deugo Nesa Matic

2 © 2003-2004, Espirity Inc.

Additional Contributors None as of Septembe, 2004

Page 3: Object-Oriented Principals Dwight Deugo Nesa Matic

3 © 2003-2004, Espirity Inc.

Module Overview1. Object-Oriented Principals

Page 4: Object-Oriented Principals Dwight Deugo Nesa Matic

4 © 2003-2004, Espirity Inc.

Module Road Map1. Object-Oriented Principals

Objects Classes, instances, fields and methods Encapsulation Polymorphism Inheritance Dynamic binding

Page 5: Object-Oriented Principals Dwight Deugo Nesa Matic

5 © 2003-2004, Espirity Inc.

What is Object-Oriented Technology? It is a technology that allows modeling

of software as the real world This is achieved through objects and

interactions between them It is a way of thinking of software as

reusable components Every object is a component on its own

and could be reused in different context

Page 6: Object-Oriented Principals Dwight Deugo Nesa Matic

6 © 2003-2004, Espirity Inc.

Why Use Object-Oriented Systems? Benefits of Object-Oriented systems:

Greater flexibility Reusability of objects Higher quality control Lower maintenance

Page 7: Object-Oriented Principals Dwight Deugo Nesa Matic

7 © 2003-2004, Espirity Inc.

Developing Object-Oriented Software Software is modeled by objects

Software objects represent real-world objects

Each object contains: State, i.e., data Behavior, i.e., methods/functions

Page 8: Object-Oriented Principals Dwight Deugo Nesa Matic

8 © 2003-2004, Espirity Inc.

Object-Oriented Software Developing object-oriented software is

identifying: Objects Characteristics of individual objects Relationships between objects

Objects interact by sending messages to each other Interacting objects make an object-

oriented system

Page 9: Object-Oriented Principals Dwight Deugo Nesa Matic

9 © 2003-2004, Espirity Inc.

Objects and Loose Coupling Changing an object’s data does not

lead to changes in an object’s external behavior An object’s external interface stays the

same Promotes loose coupling between objects

state

behavior

object1 state

behavior

object2

state

behavior

object3

Page 10: Object-Oriented Principals Dwight Deugo Nesa Matic

10 © 2003-2004, Espirity Inc.

Objects Every object has:

State Behavior

State represents data - what an object knows, or what an object contains

Behavior represents what an object can do

Page 11: Object-Oriented Principals Dwight Deugo Nesa Matic

11 © 2003-2004, Espirity Inc.

Person object State: age, name, children Behavior: addChild, getAge, setAge

Object State and Behavior

statebehavior

aPersonagenamechildren

getAgesetAgeaddChild…

Page 12: Object-Oriented Principals Dwight Deugo Nesa Matic

12 © 2003-2004, Espirity Inc.

Interactions between Objects Object interact by sending messages to

each other Objects and interactions between them

make up an object-oriented systemaPolicyFactory:

createPolicy

aBroker: aPerson:

aPolicy:aPolicy

new(“Joe Smith”, “35 years”)aPerson

setOwner(aPerson)

Page 13: Object-Oriented Principals Dwight Deugo Nesa Matic

13 © 2003-2004, Espirity Inc.

Messages There are two major terms in

messaging: Message sender Message receiver

Messages may have arguments

getAge()sender receiver

message

aBroker: aPerson:

Page 14: Object-Oriented Principals Dwight Deugo Nesa Matic

14 © 2003-2004, Espirity Inc.

Methods Method is concrete implementation of

a message When message is sent to a receiver:

Method is found by type of the receiver object and method signature

Method code is executed Method represents an object’s

response to a message

Page 15: Object-Oriented Principals Dwight Deugo Nesa Matic

15 © 2003-2004, Espirity Inc.

Method Signature Method signature is unique identifier of

the method It is used to distinguish methods with

same name and same number of parameters

It consists of: Method name Parameter name Parameter type

Page 16: Object-Oriented Principals Dwight Deugo Nesa Matic

16 © 2003-2004, Espirity Inc.

Object’s Public Protocol Public protocol is set of messages that

can be sent to an object It does not include messages that an

object can send to itself These are private aPerson

agenamechildren

getAgesetAgeaddChild…

public protocol

Page 17: Object-Oriented Principals Dwight Deugo Nesa Matic

17 © 2003-2004, Espirity Inc.

Fields Fields represent characteristics of an

object Fields are also known as attributes, or

instance variables aPersonagenamechildren

getAgesetAgeaddChild…

fields

Page 18: Object-Oriented Principals Dwight Deugo Nesa Matic

18 © 2003-2004, Espirity Inc.

Object-Oriented Principal: Encapsulation

Objects hide implementation of the messages behind their public protocols Object’s internal implementation is

accessed by that object only Encapsulation is also known as

implementation hiding

getName()

aBroker: aPerson:

getAge()Joe Smith

35

Page 19: Object-Oriented Principals Dwight Deugo Nesa Matic

19 © 2003-2004, Espirity Inc.

Classes Classes are:

Factories for creating objects Template for the same kind of

objects that describes their state and behavior

Code repository for objects Classes define objects (by

defining their state and behavior) and their type

PersonagenamechildrenaddressgetAgesetAgeaddChild…

Page 20: Object-Oriented Principals Dwight Deugo Nesa Matic

20 © 2003-2004, Espirity Inc.

Instances Every object is an instance of some

class All instances of same class have the

same protocol They have same fields and same methods

that are defined by the classJohn Smith35John SmithJimmy, Chloe

Dad Smith60Dad SmithJohn, Tom

class

classPersonagenamechildrenaddressgetAgesetAgeaddChild…

Page 21: Object-Oriented Principals Dwight Deugo Nesa Matic

21 © 2003-2004, Espirity Inc.

Object-Oriented Principal: Inheritance

Some classes may share commonalities For example HomePolicy, AutoPolicy, LifePolicy classes may all have same state and behavior

Instead of repeating commonalities in each class, we can abstract them in a common place These commonalities can be stored in a

super class Each subclass inherits state and behavior

from its superclass

Page 22: Object-Oriented Principals Dwight Deugo Nesa Matic

22 © 2003-2004, Espirity Inc.

Specialization and GeneralizationPolicy

clientpremiumpolicyNumbergetClientsetClient…

HomePolicyhouse

getHousesetHouse

AutoPolicyauto

getAutosetAuto

LifePolicy

gene

raliz

atio

nspecialization

Page 23: Object-Oriented Principals Dwight Deugo Nesa Matic

23 © 2003-2004, Espirity Inc.

Why Inheritance? Inheritance represents real-world

modeling Some objects are special cases of other

objects Inheritance promotes reuse and

extensibility Same data and behavior is shared among

objects of different types (different class) New data and new behavior that is

common for those objects is easier to add

Page 24: Object-Oriented Principals Dwight Deugo Nesa Matic

24 © 2003-2004, Espirity Inc.

Object-Oriented Principal: Polymorphism

Polymorphism different objects respond to the same

message in different ways For example when asked to talk a dog barks,

and a cat meows It is often supported by method overriding

Overriding means that subclass may implement the same method as superclass, but with different code

toString() method in the Object class is an example of often overridden method

Page 25: Object-Oriented Principals Dwight Deugo Nesa Matic

25 © 2003-2004, Espirity Inc.

Overriding Example Consider Animal class:

Dog and Cat as subclasses All Animal objects should know how to

talk

aCat.talk();

aDog.talk();

meowsbarks

Animaltalk()

Dogtalk()

Cattalk()

Page 26: Object-Oriented Principals Dwight Deugo Nesa Matic

26 © 2003-2004, Espirity Inc.

Dynamic Binding Dynamic binding represents runtime

method binding It is runtime binding of method invoked in

the message to the method that implements that message

For example:anAnimal.talk();

aDog…

talk

aCat…

talk

Page 27: Object-Oriented Principals Dwight Deugo Nesa Matic

27 © 2003-2004, Espirity Inc.

Labs!

Lab: Object-Oriented Concept