Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Gentle Introduction to (Object-Oriented) Programming

Similar presentations


Presentation on theme: "A Gentle Introduction to (Object-Oriented) Programming"— Presentation transcript:

1 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 Eng Dept, Bilkent Univ
Computer hardware Memory Output Devices Input Devices Hard disk ALU Control Unit Registers Cache CPU Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

3 Computer Eng Dept, Bilkent Univ
Computer software Source file ADDF3 R1,R2, R3 noStudents++; Executable file Compiler / Linker Operating System Hardware Application Programs User Specific computer Specific JVM Java Bytecode Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

4 Computer Eng Dept, Bilkent Univ
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 … 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 Computer Eng Dept, Bilkent Univ
OOP basics: memory When a program is loaded into memory for execution: Heap segment Stack segment Text (code) segment Memory Heap Stack Static data Code myObj = new Object(); int i = 9; Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

8 Computer Eng Dept, Bilkent Univ
OOP basics: memory class Node { Object data; Node next; } … main(…) { List myList = new List(); Integer i = new Integer(5); myList.insertFront(i); myList.insertRear(new Integer(8); } 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) { ... } } Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

9 Computer Eng Dept, Bilkent Univ
OOP basics: memory class Node { Object data; Node next; } … main(…) { List myList = new List(); } myList first: null List 1 3 2 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) { ... } } null 123 List 456 myList Heap Stack Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

10 Computer Eng Dept, Bilkent Univ
OOP basics: memory class Node { Object data; Node next; } … main(…) { List myList = new List(); Integer i = new Integer(5); } myList first: null List i value: 5 Integer 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) { ... } } Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

11 Computer Eng Dept, Bilkent Univ
OOP basics: memory class Node { Object data; Node next; } … main(…) { List myList = new List(); Integer i = new Integer(5); myList.insertFront(i); } myList first List i value: 5 Integer data next: null Node 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) { ... } } Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

12 Computer Eng Dept, Bilkent Univ
OOP basics: memory class Node { Object data; Node next; } … 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 next: null value: 8 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) { ... } } Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

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
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){…} } Implementation details hidden Proper class interface Implementation changed 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){…} Class interface unchanged Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

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.)
List Stack StringStack +top() +push(String) +pop() +getFirst() +insertFront(Object) +insertRear(Object) +delete() +top() +push(Object) +pop() elements elements void push(Object newElement) { elements.insertFront(newElement); } void push(String newString) { elements.push(newString); } IncrStringStack String pop() { (String)(elements.pop()); } Reuse through Delegation +push(String) void push(String newString) { String top = getTop(); if (top==null || newString.compareTo(top)>0) super.push(newString); } Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

17 OOP basics: polymorphism
Entries in this file system are either files or folders or links to other entries Files contain text Single root Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

18 OOP basics: polymorphism
Composite Design Pattern Abstract methods needed for polymorphic use Overriden in sub-classes for specific behavior 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; Example use of polymorphism Ugur Dogrusoz Computer Eng Dept, Bilkent Univ

19 Computer Eng Dept, Bilkent Univ
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, Ugur Dogrusoz Computer Eng Dept, Bilkent Univ


Download ppt "A Gentle Introduction to (Object-Oriented) Programming"

Similar presentations


Ads by Google