a gentle introduction to ( object-oriented) programming ugur dogrusoz associate professor, computer...

19
A Gentle Introduction to (Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December 2011 Computer Eng Dept, Bilkent Univ

Upload: kaitlynn-carley

Post on 14-Dec-2015

225 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

A Gentle Introduction to

(Object-Oriented) Programming

Ugur DogrusozAssociate Professor, Computer Eng Dept,Bilkent University, Ankara, Turkey

December 2011 Computer Eng Dept, Bilkent Univ

Page 2: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

2

Computer hardware

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

Memory

Output Devices

Input Devices

Hard disk

ALU

Control Unit

Memory

Registers Cache

CPU

Page 3: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

3

Computer software

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

Operating System

Hardware

Application Programs

User

Source file

ADDF3 R1,R2, R3noStudents++;

Executable file

11000101011000100011101001

1

Compiler / Linker

Specific computer

Specific JVM

Java Bytecode

Page 4: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

4

How does a program work

A program tells a computer what to do, step by step, to get it to do something useful

Computers’ power comes from (nothing more than) Accuracy Speed

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

Page 5: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

5

Programming languages

Machine Code 101110101… Lowest level: most efficient but (almost) humanly

impossible to directly deal with

Assembly Language ADDF3 R1,R2, R3

Compiler / Interpreter Language noStudents++ Highest level: easier for humans to express their

instructions in

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

Page 6: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

6

Object-oriented (OO) prog. languagesAs opposed to procedural / functional /

imperative approachHigh level general purpose prog. languagesEach object (modeled after its real-life equivalent) has

certain properties and responsibilitiesAbstraction, encapsulation, polymorphism,

reuse, maintenanceOO software = interacting collection of objects

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

Page 7: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

7

OOP basics: memory

When a program is loaded into memory for execution:

Heap segment

Stack segment

Text (code) segment

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

Memory

Heap

Stack

Static dataCode

myObj = new Object();

int i = 9;

Page 8: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

8

OOP basics: memory

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

class Node { Object data; Node next;}

class List { private Node first; public Object getFirst() {return(first.data);} public void insertFront(Object newElement) { ... } public void insertRear(Object newElement) { ... } public void delete(Object existingElement) { ... }}

… main(…) { List myList = new List(); Integer i = new Integer(5); myList.insertFront(i); myList.insertRear(new Integer(8);}

Page 9: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

9

OOP basics: memory

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

class Node { Object data; Node next;}

class List { private Node first; public Object getFirst() {return(first.data);} public void insertFront(Object newElement) { ... } public void insertRear(Object newElement) { ... } public void delete(Object existingElement) { ... }}

… main(…) { List myList = new List();

}

myList

first: null List

null123 List

123456 myList

1

23

Heap

Stack

Page 10: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

10

OOP basics: memory

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

class Node { Object data; Node next;}

class List { private Node first; public Object getFirst() {return(first.data);} public void insertFront(Object newElement) { ... } public void insertRear(Object newElement) { ... } public void delete(Object existingElement) { ... }}

… main(…) { List myList = new List(); Integer i = new Integer(5); } myList

first: null List

i

value: 5 Integer

Page 11: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

11

OOP basics: memory

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

class Node { Object data; Node next;}

class List { private Node first; public Object getFirst() {return(first.data);} public void insertFront(Object newElement) { ... } public void insertRear(Object newElement) { ... } public void delete(Object existingElement) { ... }}

… main(…) { List myList = new List(); Integer i = new Integer(5); myList.insertFront(i); }myList

first Listi

value: 5 Integer

datanext: null Node

Page 12: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

12

OOP basics: memory

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

class Node { Object data; Node next;}

class List { private Node first; public Object getFirst() {return(first.data);} public void insertFront(Object newElement) { ... } public void insertRear(Object newElement) { ... } public void delete(Object existingElement) { ... }}

… main(…) { List myList = new List(); Integer i = new Integer(5); myList.insertFront(i); myList.insertRear(new Integer(8);}

myList

first Listi

value: 5 Integer

datanext Node

datanext: null Node

value: 8 Integer

Page 13: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

13

OOP basics: encapsulation/info hidingExpose only what’s necessaryHide implementation details

Unnecessary information should not be visible or modifiable

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

Page 14: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

14

OOP basics: encapsulation/info hiding

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

class MyRectangle { private double width; private double height; private Point leftTop;

public double getWidth(){ return width; } public void setWidth(double width){this.width=width;} public void translate(double deltaX, double deltaY){…}}

class MyRectangle { private Rectangle rect;

public double getWidth(){ return rect.getWidth(); } public void setWidth(double width){ rect.setSize(width, rect.getHeight()); } public void translate(double deltaX, double deltaY){…}}

Implementation details hidden

Proper class interface

Implementation changed

Class interface unchanged

Page 15: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

15

OOP basics: reuse (inherit. & compo.)Two main forms of re-use in software eng.:

Inheritance: ask whether instance of sub-class is-a(n) instance of super-class

Composition: ask whether whole has-a(n) part makes sense

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

Page 16: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

16

OOP basics: reuse (inherit. & compo.)

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

List

+getFirst()+insertFront(Object)+insertRear(Object)

+delete()

IncrStringStack

+push(String)

void push(Object newElement) { elements.insertFront(newElement);}

Stack

+top()+push(Object)

+pop()elements

StringStack

+top()+push(String)

+pop()elements

void push(String newString) { elements.push(newString);}

void push(String newString) { String top = getTop(); if (top==null || newString.compareTo(top)>0) super.push(newString);}

Reuse through Delegation

String pop() { (String)(elements.pop());}

Page 17: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

17

OOP basics: polymorphism

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

Entries in this file system are either files or folders or links to other entries

Files contain textSingle root

Page 18: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

18

OOP basics: polymorphism

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

public File search(String aString) { if (!this.isBroken()) return this.linkTo.search(aString); return null;}

public File search(String aString) { if (this.contents.indexOf(aString) >= 0) return this; return null;}

public File search(String aString) { for (Entry anEntry : this.entries) { File aFile = anEntry.search(aString); if (aFile != null) return aFile; } return null;}

Abstract methods needed for

polymorphic use

Overriden insub-classes for

specific behavior

Composite Design Pattern

Example use of polymorphism

Page 19: A Gentle Introduction to ( Object-Oriented) Programming Ugur Dogrusoz Associate Professor, Computer Eng Dept, Bilkent University, Ankara, Turkey December

19

References

OOSE, Using UML, Patterns, and Java, 3rd Edition by Bernd Bruegge and Allen H. Dutoit, Prentice-Hall, 2010

Java Software Solutions, Foundations of Program Design by John Lewis and William Loftus, Addison-Wesley, 1998

Wikipedia, http://en.wikipedia.org

Ugur Dogrusoz Computer Eng Dept, Bilkent Univ