36
Introduction to OOP Mehdi Einali Advanced Programming in Java 1

Introduction to OOP Mehdi Einali Advanced Programming in Java 1

Embed Size (px)

Citation preview

Page 1: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

1

Introduction to OOP

Mehdi Einali

Advanced Programming in Java

Page 2: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

2

agendaObject Oriented ProgrammingCharacteristics of objectsInterfaceEncapsulation

Page 3: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

3

AbstractionSimplification of complex system via interface(Modeling)

Machine languageAssembly: an abstraction of the machine languageMany languages are abstraction of assembly language

Fortran, Basic, C

But they still require you to think in terms of the structure of the computer Rather than the structure of the problem

Page 4: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

4

Different ContextsProblem Space

the place where the problem existssuch as a business

Solution Spacethe place where you’re implementing that solutionsuch as a computer

The effort required to perform this mapping

Page 5: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

5

Library ProblemSuppose you want to write a library programWhat are the elements of your program?We think about methods and variables…

Page 6: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

6

Object Oriented ApproachOO approach goes a step further Lets the programmer represent problem space elementsThis representation is general enough The programmer is not constrained to any particular type of problem.

The elements in the problem space and their representations in the solution space are referred to as “objects”

Page 7: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

7

OOPThe program is allowed to adapt itself to the lingo of the problem

by adding new types of objects

when you read the code, you’re reading words that also express the problem. This is a more flexible and powerful language abstraction

Page 8: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

8

Reuse

Student

Course

Teacher

Grant

Book

Paper

Journal

Education System

Library System

Research System

Page 9: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

9

ChangeSW system could not change with

requirements

Maintenance is more expensive than development

Avoid cascading changes

Increase cohesion / decrease coupling

Object Orientation

Page 10: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

10

Advantage of OOPAbstractionSeamless in modeling

Best fit to problem domain

ReuseGood for Change

ExtendibilityMaintainabilityModifiabilityTestability

Page 11: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

11

generation 1First-generation languages (1954–1958)

FORTRAN I Mathematical expressionsALGOL 58 Mathematical expressionsFlowmatic Mathematical expressionsIPL V Mathematical expressions

This first generation of high-order programming languages therefore represented a step closer to the problem space and a step further away from the underlying machine

Page 12: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

12

Generation 2Second-generation languages (1959–1961)

FORTRAN II Subroutines, separate compilationALGOL 60 Block structure, data typesCOBOL Data description, file handlingLisp List processing, pointers, garbage collection

Emphasis was on algorithmic abstractionsMachines were becoming more and more powerful then more kinds of problems could be automated, especially for business applications

read these personnel records first, sort them next, and then print this report

A step closer to the problem space and further away from the underlying machine

Page 13: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

13

generation 3Third-generation languages (1962–1970)

PL/1 FORTRAN + ALGOL + COBOLALGOL 68 Rigorous successor to ALGOL 60Pascal Simple successor to ALGOL 60Simula Classes, data abstraction

Advent of transistors and then integrated circuit technology, the cost of computer hardware had dropped dramaticallyLarger problems could now besolved, but these demanded the manipulation of more kinds of dataSupport data abstraction

describe the meaning of related kinds of data (their type)

Page 14: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

14

Genertion gapThe generation gap (1970–1980)

Many different languages were invented, but few endured. However, the following are worth noting:C Efficient; small executableFORTRAN 77 ANSI standardization

Creation of literally a couple of thousand different programming languages and dialectsFew of these languages survived

Page 15: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

15

oopObject-orientation boom (1980–1990, but few languages survive)

Smalltalk 80 Pure object-oriented languageC++ Derived from C and SimulaAda83 Strong typing; heavy Pascal influenceEiffel Derived from Ada and Simula

Page 16: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

16

FrameworksEmergence of frameworks (1990–today)

Visual Basic Eased development of the graphical user interface (GUI) for Windows applicationsJava Successor to Oak; designed for portabilityPython Object-oriented scripting languageJ2EE Java-based framework for enterprise computing.NET Microsoft’s object-based frameworkVisual C# Java competitor for the Microsoft .NETFramework

Visual Basic .NET Visual Basic for the Microsoft .NET Framework

Page 17: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

17

Topology of 1st and early 2nd gen

Basic physical building block of all applications is the subprogram (or the paragraph - COBOL)Flat physical structure, consisting only of global data and subprograms

An error in one part of a program can have a devastating ripple effect across the rest of the system

Page 18: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

18

Late 2nd and Early 3rd Gen

The first software abstraction, now called the ‘procedural’ abstraction

Variety of parameter-passing mechanismsNesting subprogram, theories about control structures, scope and visibility of structuresStructured design methods

Greater control over algorithmic abstractionsStill fails to address the

problems of programming-in-the-large and data design.

Page 19: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

19

Late Third-GenerationLarger programming projects meantlarger development teams, and thus the need to develop different parts of the same program independentlySeparately compiled module Need for semantic

consistency among module interfaces

Most of these languages had dismal support for dataabstraction and strong typing, such errors could be detected only during executionof the program.

Page 20: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

20

Object based and object oriented

Procedural abstraction is not enoughData abstraction is important to mastering complexity

Data-driven design methods emerged, which provided a disciplined approach to the problems of doing data abstraction in algorithmically oriented languagesTheories regarding the concept of a typeappeared, which eventually found their realization in languages such as Pascal

If procedures and functions are verbs and piecesof data are nouns, a procedure-oriented program is organized around verbs while an object-oriented program is organized around nouns

Page 21: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

21

Beyond programming languages

Enterprise Application Integration(EAI)Service Oriented architecture(SOA)

Page 22: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

22

OOP vs. Procedural Approach

Elements of OOPObjectsMessage passing between objects

Elements of procedural programmingFunctions VariablesFunction invocation

The way of thinkingThinking about objects and relationsThinking about functions and computer structure

Page 23: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

23

OOP CharacteristicsAlan Kay summarized five basic characteristics of Smalltalk

1. Everything is an object 2. A program is a bunch of objects telling each

other what to do by sending messages

3. Each object has its own memory made up of other objects

4. Every object has a type 5. All objects of a particular type can receive the

same messages

Page 24: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

24

Booch’s description of an Object

An object has state, behavior and identityBooch added identity to the descriptionAn object (may) have internal data

which gives it state

An object (may) have methods to produce behavior

And each object can be uniquely distinguished from every other object

Each object has a unique address in memory

Page 25: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

25

InterfaceEach object can satisfy only certain requestsThe requests you can make of an object are defined by its interfaceThe type is what determines the interface

Page 26: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

26

Representation of a light bulb

Page 27: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

27

Person in an Education System

Page 28: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

28

New names in OOPFunction Method, ServiceVariable Property, State

Page 29: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

29

EncapsulationCommercial products are encapsulated

Remote controlTVCell phone

They are Black BoxesHidden ImplementationsPublic interface

Page 30: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

30

Why Encapsulation?Simplified use

Even for the producer

Open implementation bad useHiding the implementation reduces bugsIt is more beautiful!

Page 31: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

31

Object EncapsulationEncapsulation of a problem-space concept into a class of objectsDefine interfaceHide the implementationBlack boxThe client may see the implementationBut can not use it directlyThis is better even for the producer (programmer)

Page 32: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

32

Access ControlAccess to some parts of the class is restrictedPublic and Private area of the classThe client of a class can use only public areaPublic area = class interface

Public methodsPublic variables

Page 33: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

33

Example: RectangleLets encapsulate a rectangleWhat is a rectangle?

An objectWhich has length and width (properties)Lets you specify its length and widthCan calculate its area and perimeter

Page 34: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

34

Class Declarationpublic class Rectangle {

private int width, length;

public void setWidth(int w) {width = w;

}public void setLength(int l) {

length = l;}public int calculateArea(){

return width*length;}public int calculatePerimeter(){

return (width+length)*2;}

}

Private area: hidden implementation

Public area :the interface

Page 35: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

35

How to Use Rectangle?

Rectangle rect = new Rectangle();

rect.setWidth(2);rect.setLength(7);

System.out.println(rect.calculateArea());System.out.println(rect.calculatePerimeter());

Object creation or instantiation

Page 36: Introduction to OOP Mehdi Einali Advanced Programming in Java 1

36

Grady BoochBooch Method

Three Amigos(1994-95)Ivar Jacobson

Object Modeling Technique(OMT)

RumbaughProcess Objectory

Unified Modeling Language(UML)Unified Process(UP)Object Oriented Analysis and

Design-3rd edtion(2007)