Introduction to Object-Oriented Programming (OOP)

Download Report

Transcript Introduction to Object-Oriented Programming (OOP)

Introduction 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)
Why OOP?
int stack[100];
int top;
void init(){
top = -1;
}
void push(int val){
if (!isFull()){
stack[++top] = 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
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 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
Abstract Data Types (ADT)
 ADT = (V,O)
– V: a set of values
– O: a set of operators
 Information hiding
 Encapsulation
 Modularity
Stack 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;
}
Elements 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
Elements of OOP
 Objects
– An object has its own state
– An object’s behavior is determined by its class
 Methods
– Operations on objects
Elements of OOP
 Messages
– Objects perform computation by sending
messages to each other
message:
receiver
method name
parameters
return value
SENDER
RECEIVER
Elements 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
Elements of OOP
-- Recursive Design
 Every object has its own memory, which
stores other objects
Inheritance
super-class
ParkingMeter
subclass or
extended class
DigitalParkingMeter AnalogParkingMeter
Elements of OOP--Overriding
 Subclasses can alter or override information
inherited from super-classes
Bird
NotFlyingBird
Penguin
Why 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
Java is not Cure-all
 Database
– Database programming languages
 Artificial intelligence
– Lisp, Prolog, Constraint languages
 CGI
– Perl, Java script, TCL
 Many other domain-specific languages