Download presentation
Presentation is loading. Please wait.
1
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)
2
Why OOP? What are the problems? Not generic Fixed size
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
3
Why OOP? What are the problems? Still not generic Fixed size
#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
4
Abstract Data Types (ADT)
ADT = (V,O) V: a set of values O: a set of operators Information hiding Encapsulation Modularity
5
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;
6
Elements of OOP OOP Programming Classes
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
7
Elements of OOP Objects Methods An object has its own state
An object’s behavior is determined by its class Methods Operations on objects
8
Elements of OOP Messages
Objects perform computation by sending messages to each other message: receiver method name parameters return value SENDER RECEIVER
9
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
10
Elements of OOP -- Recursive Design
Every object has its own memory, which stores other objects
11
Inheritance super-class ParkingMeter subclass or extended class
DigitalParkingMeter AnalogParkingMeter
12
Elements of OOP--Overriding
Subclasses can alter or override information inherited from super-classes Bird NotFlyingBird Penguin
13
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
14
Java is not Cure-all Database Artificial intelligence CGI
Database programming languages Artificial intelligence Lisp, Prolog, Constraint languages CGI Perl, Java script, TCL Many other domain-specific languages
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.