Lecture 1- OO-Basic [Compatibility Mode]

download Lecture 1- OO-Basic [Compatibility Mode]

of 71

Transcript of Lecture 1- OO-Basic [Compatibility Mode]

The Nelson Mandela African Institute of Science and Technology ICSE 6202: Information Systems Analysis, Design and Modeling March April 2012 Lecture 1: Basics of Object OrientedLoserian S. Laizer Email: [email protected], [email protected] 0713 477210/0787 477210 Office: Wing D, Room 105

NM-AIST - Nelson Mandela African Institute of Science and Technology - 2012

1

ObjectivesUpon completion of this section, you are expected to : 1. Explain the basic concepts of Object Oriented technology. 2. Understand the advantages of Object Oriented system development.

2

Object Oriented Basics

ContentsChapter 1. What Object Orientation is Chapter 2. Basic Concepts of OO Technology Chapter 3. Advantages of OO Technology Chapter 4. Object Oriented Programming

3

Object Oriented Basics

1. What Object Orientation is

In this chapter, you will learn ... 1.1 Object 1.2 Object Orientation

1-4

1. What Object Orientation is

1.1 ObjectPhysical things (Car, Camera, etc.) OBJECTS Abstract things (Job, Weather, etc.)

The real world consists of Objects.

1-5

1.1 Object

1.2 Object Orientation (1/2)

Skilled Painter's approach

Conventional approach

Object Oriented approach

1-6

1.2 Object Orientation

1.2 Object Orientation (2/2)Piece Piece Object

Object

Difficult to keep coordinationObject

Easy modification !

Conventional approach

Object oriented approach

1-7

1.2 Object Orientation

2. Basic Concepts of OO TechnologyIn this chapter, you will learn ... 2.1 Object 2.2 Class 2.3 Inheritance & Override 2.4 Instance 2.5 Message 2.6 Polymorphism

2-8

2. Basic Concepts of OO Technology

2.1 ObjectObject name Data

Method

Jukebox Record disks Titles of music play music stop music accept a coin

A system consists of Objects. Objects consist of Data and Methods. The data and the methods in an object are strongly related.

2-9

2.1 Object

EncapsulationJukebox Record disks Titles of music play music stop music accept a coin Necessary data for this Object Necessary methods for this Object

Data and Methods are encapsulated as an object. We dont have to know the internal structure of the object.

2-10

2.1 Object

2.2 ClassClass name Data Method Car owner type color speed up slow down turn

Katsra's car

Ryo's car

Shuji's car

Keisuke's car

2-11

2.2 Class

Generalization/SpecializationGeneralization -Super-class Vehicle

Car Specialization -Sub-class Convertible Sedan

Truck

: Is a or Is a kind of Relationship Example : A Car is a kind of Vehicle.2-12 2.2 Class

AggregationA class has or contains another class.contains or is composed of

CAR Whole

SEAT Part

ComputerA computer has CPU, memory and disk.

CPU2-13

Memory2.2 Class

Disk

2.3 Inheritance & OverrideSuperclassVehicle owner type color speed up slow down turn

Data and methods are inherited to the subclasses.

SubclassTaxi customer carry customerPeculiar data Peculiar method

SubclassTruck cargo transport cargo

2-14

2.3 Inheritance & Override

Overrideemployee_info name birthday age address calc_age(birthday) calc_salary(age)

inheritanceexecutive_info post calc_salary(age)

Override: Same name, but different function

2-15

2.3 Inheritance & Override

(Reference) Overloademployee_info name birthday age post address calc_age(birthday) calc_salary(age) calc_salary(age,post)

Overload: Same method name, with different arguments

2-16

2.3 Inheritance & Override

2.4 InstanceCLASSCar owner type color speed up slow down turn Class is used as a mold to make an INSTANCE.

Construction of Instance INSTANCE ownerRyo's car : Car : Ryotype : Sports car color : RED speed up slow down turn

Private Data

2-17

2.4 Instance

2.5 Message

MESSAGE is an interface between Objects. When an Object asks another Object to do something, the first Object sends a MESSAGE to the second Object.

2-18

2.5 Message

Example of MessageMESSAGE is an interface between objects.

mike.go_to(Tengeru)

diana :Customer Age : 29 Sex : Female speak(string) commute(string)

mike :Taxi Driver Age : 45 Sex : Male speak(string) go_to(string)

2-19

2.5 Message

2.6 Polymorphism We use the same verb in different Objects to do different behaviors. (e.g. We use OPEN to open a window and a data file. The ways to open are different, but the verb is the same.) That is called POLYMORPHISM. Its an abstraction of Message.

2-20

2.6 Polymorphism

Example of Polymorphismrio : Cat cry() attack() move() MIAOWScratch Jump

Kei:Master

hamu.cry()

hamu : Dog cry() attack() move() ku-chi : Parakeet cry() attack() move() PIYOPIYOPPick Fly

BOWWOWBite Run

The messages are the same cry(). But the behaviors are different.2-21

2.6 Polymorphism

SummaryIn this chapter, you have studied about ... Object, Encapsulation Class Generalization, Specialization, Aggregation Inheritance, Override Instance Message Polymorphism2-22 2. Basic Concepts of OO Technology

Drill 2-1EncapsulationEncapsulate the following data items and methods into 2 groups and make 2 objects. Write your answer on the next page. Object nameBank account Foreign exchange

DataCustomer_name Account_Number Yen Dollar Balance Exchange_rate

MethodWithdraw_cash Exchange_Dollar_to_TZS Exchange_TZS to_Dollar Deposit_cash

Object name

ExampleData2-23 2. Basic Concepts of OO Technology

Methods

Drill 2-1Answer

2-24

2. Basic Concepts of OO Technology

Drill 2-2

Inheritance Make a class hierarchy chart with the following words. You can use each word only once.Class name Personal_info Data name address telephone_number birthday2-26

Customer_info

Employee_info

salary position department amount_billed

2. Basic Concepts of OO Technology

Drill 2-3

Aggregation Make Object model of the following objects. Object Model of a Car Tire Car Engine Door

2-27

2. Basic Concepts of OO Technology

3. Advantages of OO TechnologyIn this chapter, you will study about ... 3.1 Problems in Current System Development 3.2 OO Technology Characteristics 3.3 OO System Development 3.4 What is OO Heading for ...

3-29

3. Advantages of OO Technology

3.1 Problems in Current System Development

Software Crisis1) Inferior QualitySpecifications not fully met

2) Decreased ProductivityProlonged period of development

3) Lower MaintainabilityDifficult to maintain due to complexity in structure

3-30

3.1 Problems in Current System Development

3.2 OO Technology CharacteristicsCharacteristics of OO technology 1) Reuse of software Difference programming Class Library 2) Polymorphism 3) Data hiding 4) Iterative and Incremental development3-31 3.2 OO Technology Characteristics

3.3 OO System DevelopmentPick up Objects from target business. Make fundamental data and methods. Necessary data and methods are added step by step, with repeating short cycle of analysis, design, programming, and testing. Working target is always object throughout the development process. When design is done, implementation and testing are performed

Iterative and Incremental development

AccountInt x, y getPassword(aN){ }

AccountInt x, y getPassword(aN){ } calcBalance(aN){ }

import java.applet.*; import java.awt.*; public class Account extends Applet{ int x, y; public void getPassword(aN) { } public static calcBalance(xxx) }

Cut Over

3-32

3.3 OO System Development

3.4 What is OO Heading for ...Characteristics of OO technology Target of system development

Reuse of software Polymorphism Data hidingIterative and Incremental development

Improved Quality Improved Productivity Improved Maintainability

3-33

3.4 What is OO Heading for

4. Object Oriented Programming

In this section, you will learn ... 4.1 OO Programming Languages 4.2 JAVA Language 4.3 Object Oriented Programming

4-35

4. Object Oriented Programming

4.1 OO Programming LanguagesSmalltalk-80Pure object oriented language developed by XEROX PaloAlto Research lab.

EiffelPure object oriented language developed by Interactive Software Engineering

C++Extended language from C developed by AT & T Bell lab.

JavaC++ like object oriented language developed by Sun Microsystems. Portable, Network, Architecture independent.

4-36

4.1 OO Programming Languages

4.2 JAVA Language4.2.1 Language Features 4.2.2 Online Documents

4-37

4.2 JAVA Language

4.2.1 Language FeaturesObject Oriented C or C++ like Simple Architecture independent

4-38

4.2.1 Language Features

4.2.2 Online DocumentsJava Technology homepagehttp://java.sun.com

The Java Language Specificationhttp://java.sun.com/docs/books/jls/index.html

Java API Documentationhttp://java.sun.com/j2se/1.4.2/docs/api/index.html

Java Language Tutorialhttp://java.sun.com/docs/books/tutorial/index.html

4-39

4.2.2 Online Documents

4.3 Object Oriented Programming4.3.1 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 First Step(Application, Applet) Class Packages Instance Encapsulation Polymorphism Others

4-40

4.3 Object Oriented Programming

4.3.1 First Step(Application, Applet) : Example1 (1/2)

Application1) Example1. java// A first program public class Example1 { public static void main(String argv[]){ int i; for(i=1;i