Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sadegh Aliakbary Sharif University of Technology Fall 2011.

Similar presentations


Presentation on theme: "Sadegh Aliakbary Sharif University of Technology Fall 2011."— Presentation transcript:

1 Sadegh Aliakbary Sharif University of Technology Fall 2011

2 Agenda Object Oriented Programming Characteristics of objects Interface Encapsulation The notes are mainly extracted from TIJ book Codes of this slide are pseudocodes they may have syntactic or logical errors Fall 2011Sharif University of Technology2

3 Abstraction Machine language Assembly: an abstraction of the machine language Many languages are abstraction of assembly language Fortran, Basic, C Big improvement But they still require you to think in terms of the structure of the computer. Rather than the structure of the problem. Fall 2011Sharif University of Technology3

4 Different Contexts Problem Space the place where the problem exists such as a business Solution Space the place where you’re implementing that solution such as a computer The effort required to perform this mapping. E.g. think about a library, or a phonebook program Name problem space and solution space entities. Fall 2011Sharif University of Technology4

5 Alternatives to model the machine An alternative is to model the problem Some early languages chose particular views of the world: LISP  All problems are ultimately lists APL  All problems are algorithmic Prolog  Casts all problems into chains of decisions. Languages have been created for constraint-based programming Each of these approaches may be a good for particular class of problems But not for all problems… Fall 2011Sharif University of Technology5

6 Library Problem Suppose you want to write a library program What are the elements of your program? We think about functions and variables… Fall 2011Sharif University of Technology6

7 Object Oriented Approach OO approach goes a step further Lets the programmer represent problem space elements This 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” Fall 2011Sharif University of Technology7

8 OOP The 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 Fall 2011Sharif University of Technology8

9 OOP (2) OOP allows you to describe the problem in terms of the problem Rather than in terms of the computer Objects in your code are similar to real objects Recall the sample programs: phonebook and library Fall 2011Sharif University of Technology9

10 Object Oriented Languages Smalltalk The first successful object-oriented language One of the languages upon which Java is based Java C++ C## Fall 2011Sharif University of Technology10

11 OOP vs. Procedural Approach Elements of OOP Objects Message passing between objects Elements of procedural programming Functions Variables Function invocation The way of thinking Thinking about objects and relations Thinking about functions and computer structure Fall 2011Sharif University of Technology11

12 OOP Characteristics Alan 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 Fall 2011Sharif University of Technology12

13 Everything is an object You can take any conceptual component in the problem dogs, buildings, services, … And represent it as an object in your program. Example int number; Person p; Book book; Think of an object as a variable It stores data But you can make requests to that object asking it to perform operations on itself. Fall 2011Sharif University of Technology13

14 Object Messages To make a request of an object, you send a message to that object. Message = invoking a method of an object. Example Book b; if(b.isReserved())… Person p; p.setPhoneNumber(66166601) Fall 2011Sharif University of Technology14

15 Each object has its own memory You can create a new kind of object by making a package containing existing objects Thus, you can build complexity into a program while hiding it behind the simplicity of objects Book{ String name; Person reservedTo; } Fall 2011Sharif University of Technology15

16 Every object has a type Each object is an instance of a class class is synonymous with type The most important distinguishing characteristic of a class is What messages can you send to it? Person p; Person q; Person[] people; Fall 2011Sharif University of Technology16

17 Substitutability All objects of a particular type can receive the same messages An object of type circle is also an object of type shape A circle is guaranteed to accept shape messages You can write code that talks to shapes and automatically handle anything that fits the description of a shape. This substitutability is one of the powerful concepts in OOP. Inheritance Polymorphism Fall 2011Sharif University of Technology17

18 Booch’s description of an Object An object has state, behavior and identity Booch has added identity to the description. An 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. Fall 2011Sharif University of Technology18

19 Abstract Data Types Each programming language has some predefined data types int, double, char, … Creating abstract data types is a fundamental concept in object-oriented programming Abstract Data Type = Class Programmer defines a class to fit a problem You extend the programming language by adding new data types specific to your needs Fall 2011Sharif University of Technology19

20 Messages E.g. Assign a book to a person Set phone number of a person Calculate area of a shape complete a transaction draw something on the screen turn on a switch. Fall 2011Sharif University of Technology20

21 Interface Each object can satisfy only certain requests The requests you can make of an object are defined by its interface The type is what determines the interface. Fall 2011Sharif University of Technology21

22 Representation of a light bulb: Fall 2011Sharif University of Technology22 UML Diagram

23 Person in an Education System Fall 2011Sharif University of Technology23

24 New names in OOP Function  Method, Service Variable  Property, State Fall 2011Sharif University of Technology24

25 Encapsulation Commercial products are encapsulated Remote control TV Cell phone They are Black Boxes Hidden Implementations Public interface Fall 2011Sharif University of Technology25

26 Why Encapsulation? Simplified use Even for the producer Open implementation  bad use Hiding the implementation reduces bugs It is more beautiful! Fall 2011Sharif University of Technology26

27 Object Encapsulation Encapsulation of a problem-space concept into a class of objects Define interface Hide the implementation Black box The client may see the implementation But can not use it directly This is better even for the producer (programmer) Fall 2011Sharif University of Technology27

28 Access Control Access to some parts of the class is restricted Public and Private area of the class The client of a class can use only public area Public area = class interface Public methods Public variables Fall 2011Sharif University of Technology28

29 Example: Rectangle Lets encapsulate a rectangle What is a rectangle? An object Which has length and width (properties) Lets you specify its length and width Can calculate its area and perimeter Fall 2011Sharif University of Technology29

30 public 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; } Fall 2011Sharif University of Technology30 Private area: hidden implementation Public area : the interface Class Declaration

31 How to Use Rectangle? Rectangle rect = new Rectangle(); rect.setWidth(2); rect.setLength(7); System.out.println(rect.calculateArea()); System.out.println( rect.calculatePerimeter() ); Fall 2011Sharif University of Technology31 Object creation or instantiation

32 More Exercises Encapsulate these concepts: Student Footbal-Player Dog name, age : properties bite, bark : messages (actions) Fall 2011Sharif University of Technology32

33 Conclusion OOP brings us a new abstraction OOP allows you to describe the problem in terms of the problem Think in terms of the structure of the problem Rather than the structure of the computer Object has state, behavior and identity Object has data and interface Fall 2011Sharif University of Technology33

34 Further Reading Google these queries and read some pages (Wikipedia is preferred) Object Oriented Programming History of Object Oriented Programming Smalltalk UML Encapsulation Inheritance Polymorphism Fall 2011Sharif University of Technology34

35 Fall 2011Sharif University of Technology35


Download ppt "Sadegh Aliakbary Sharif University of Technology Fall 2011."

Similar presentations


Ads by Google