Title: Introduction to Object-Oriented Programming (OOP)
1Introduction to Object-Oriented Programming (OOP)
- 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)
2Why OOP?
- int stack100
- int top
- void init()
- top -1
-
- void push(int val)
- if (!isFull())
- stacktop val
- else error(stack is full)
-
- ...
- 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
3Why OOP?
- define MAX 100
- typedef struct
- int top
- int valMAX
- STACK
- typedef STACK STACK_PTR
- void init(STACK_PTR sp)
- void push(STACK_PTR sp, int x)
- int pop(STACK_PTR sp)
- int isEmpty(STACK_PTR sp)
- int isFull(STACK_PTR sp)
- 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
4Abstract Data Types (ADT)
- ADT (V,O)
- V a set of values
- O a set of operators
- Information hiding
- Encapsulation
- Modularity
5Stack is an ADT
class Stack private LinkedList elms
public Stack() elms new
LinkedList() public void push(Object
x) elms.addFirst(x) public
Object pop() if (isEmpty())
throw new RuntimeException("Stack underflow")
else return elms.removeFirst()
public boolean isEmpty() return
elms.size()0
6Elements of OOP
- OOP Programming
- Provides abstractions of both operations and data
- 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
7Elements of OOP
- Objects
- An object has its own state
- An objects behavior is determined by its class
- Methods
- Operations on objects
8Elements of OOP
- Messages
- Objects perform computation by sending messages
to each other
message receiver method name
parameters
return value
RECEIVER
SENDER
9Elements of OOP
- 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
10Elements of OOP -- Recursive Design
- Every object has its own memory, which stores
other objects
11Inheritance
super-class
ParkingMeter
subclass or extended class
DigitalParkingMeter
AnalogParkingMeter
12Elements of OOP--Overriding
- Subclasses can alter or override information
inherited from super-classes
Bird
NotFlyingBird
Penguin
13Why is OOP popular?
- Hope that it will quickly and easily lead to
increased productivity and improved reliability
(solve the software crisis) - Hope for easy transition from existing languages
- Mimic problem solving in real worlds
14Java is not Cure-all
- Database
- Database programming languages
- Artificial intelligence
- Lisp, Prolog, Constraint languages
- CGI
- Perl, Java script, TCL
- Many other domain-specific languages