Introduction to ObjectOriented Programming OOP 4 History Machine
Introduction to Object-Oriented Programming (OOP) 4 History – Machine language – Assembly language – Sub-routines and loop (Fortran) – Procedures and recursion (Algol, Pascal, C) – Modules (Modula-2, Ada) – Objects (Simula, Smalltalk, C++, Java)
Why OOP? int stack[100]; int top; void init(){ top = -1; } void push(int val){ if (!is. Full()){ stack[++top] = val; else error(“stack is full”); }. . . 4 What are the problems? – Not generic • What happens if more stacks are needed? • What happens if a stack of a different type is needed? – Fixed size – No effective information hiding mechanism
Why OOP? #define MAX 100 typedef struct { int top; int val[MAX]; } STACK; typedef STACK *STACK_PTR; void init(STACK_PTR sp); void push(STACK_PTR sp, int x); int pop(STACK_PTR sp); int is. Empty(STACK_PTR sp); int is. Full(STACK_PTR sp); 4 What are the problems? – Still not generic • What happens if a stack of another type is needed? – Fixed size – No effective information hiding or data protection
Abstract Data Types (ADT) 4 ADT = (V, O) – V: a set of values – O: a set of operators 4 Information hiding 4 Encapsulation 4 Modularity
Stack is an ADT class Stack { private Linked. List elms; public Stack() { elms = new Linked. List(); } public void push(Object x) { elms. add. First(x); } public Object pop() { if (is. Empty()){ throw new Runtime. Exception("Stack underflow"); } else return elms. remove. First(); } public boolean is. Empty() { return elms. size()==0; }
Elements of OOP 4 OOP Programming – Provides abstractions of both operations and data 4 Classes – A class specifies an abstract data type – A class can be defined based on others (inheritance) – A class defines the variables and behavior common to all objects of a certain kind
Elements of OOP 4 Objects – An object has its own state – An object’s behavior is determined by its class 4 Methods – Operations on objects
Elements of OOP 4 Messages – Objects perform computation by sending messages to each other message: receiver method name parameters return value SENDER RECEIVER
Elements of OOP 4 Receivers – Messages differ from traditional procedural calls in two very important aspects: • In a message there is a designated receiver that accepts the message • The interpretation of the message may be different, depending upon the receiver
Elements of OOP -- Recursive Design 4 Every object has its own memory, which stores other objects
Inheritance super-class Parking. Meter subclass or extended class Digital. Parking. Meter Analog. Parking. Meter
Elements of OOP--Overriding 4 Subclasses can alter or override information inherited from super-classes Bird Not. Flying. Bird Penguin
Why is OOP popular? 4 Hope that it will quickly and easily lead to increased productivity and improved reliability (solve the software crisis) 4 Hope for easy transition from existing languages 4 Mimic problem solving in real worlds
Java is not Cure-all 4 Database – Database programming languages 4 Artificial intelligence – Lisp, Prolog, Constraint languages 4 CGI – Perl, Java script, TCL 4 Many other domain-specific languages
- Slides: 14