private - Bilkent University

wakecabbagepatchΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

64 εμφανίσεις

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

2

Computer hardware

Ugur Dogrusoz

Computer Eng Dept, Bilkent Univ

Memory

Output
Devices

Input
Devices

Hard disk

ALU

Control
Unit

Memory

Registers

Cache

CPU










3

Computer software

Ugur Dogrusoz

Computer Eng Dept,
Bilkent

Univ

Operating System

Hardware

Application Programs

User

Source file

ADDF3 R1,R2, R3

noStudents++;

Executable file

11000101011000

1000111010011

Compiler
/ Linker

Specific
computer

Specific JVM



Java
Bytecode





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

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

6

Object
-
oriented (OO) prog. languages


As opposed to procedural / functional /
imperative approach


High level general purpose prog. languages


Each object
(modeled after its real
-
life equivalent)
has
certain
properties

and
responsibilities


Abstraction, encapsulation, polymorphism,
reuse, maintenance


OO software = interacting collection of objects

Ugur Dogrusoz

Computer Eng Dept, Bilkent Univ

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 data

Code

myObj =
new Object()
;

int i

= 9;

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);

}

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

null

123

List

123

456

myList







1

2

3

Heap

Stack

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

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

List

i

value: 5

Integer

data

next: null

Node

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

List

i

value: 5

Integer

data

next

Node

data

next: null

Node

value: 8

Integer

13

OOP basics: encapsulation/info hiding


Expose only what’s necessary


Hide implementation details


Unnecessary information should not be visible or
modifiable

Ugur Dogrusoz

Computer Eng Dept, Bilkent Univ

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

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

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());

}

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 text


Single root


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

in

sub
-
classes for
specific behavior

Composite
Design Pattern

Example use of
polymorphism

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