Download presentation
Presentation is loading. Please wait.
1
Object-Oriented Programming
OOP = ADT (Abstract Data Types) + Inheritence + Dynamic binding
2
Abstract Data Types (ADT)
ADT = (V,O) V: a set of variables O: a set of operations Advantages Abstraction of both data and operations Information hiding and data protection Modularity
3
An example ADT: Rectangle
Variables: int x,y,width,height Operations: Rectangle(int,int,int,int) void move(int,int) void resize(float) int area() boolean isSquare() print()
4
Implementation in Java
class Rectangle { private int x,y,width,height; public Rectangle(int x,int y,int width,int height){ this.x = x; this.y = y; this.width = width; this.height = height; } public void move(int x, int y){ this.x = x; this.y = y; public void resize(float ratio){ width = (int)(ratio*width) ; height = (int)(ratio*height); ...
5
Implementation in C++ class Rectangle { private: int x,y,width,height;
public: Rectangle(int x,int y,int width,int height){ this->x = x; this->y = y; this->width = width; this->height = height; } void move(int x, int y){ this->x = x; this->y = y; void resize(float ratio){ width = (int)(ratio*width) ; height = (int)(ratio*height); }… };
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
Using Rectangle in Java
class TestRectangle { public static void main(String[] args){ Rectangle r = new Rectangle(0,0,100,50); r.resize(1.5); r.move(100,100); r.print(); }
11
Using Rectangle in C++ main(){ Rectangle r(0,0,100,50); r.resize(1.5);
r.move(100,100); r.print(); } main(){ Rectangle* r = new Rectangle(0,0,100,50); r->resize(1.5); r->move(100,100); r->print(); }
12
Implementing LinkedList in Java
class Node { private int val; private Node next; public Node(int x, Node next){ val = x; this.next = next; } public Node(int x){ this(x,null); public void setNext(Node next){…} public Node getNext(){…} public void setVal(int x){…} public int getVal(){…}
13
Implementing LinkedList in Java
class MyLinkedList { private Node head; public void addFirst(int x){ head = new Node(x,head); } public void add(int x){ if (head == null){ head = new Node(x); } else { Node pre,curr; pre = curr = head; while (curr != null){ pre = curr; curr = curr.getNext(); pre.setNext(new Node(x));
14
Implementing LinkedList in C++
class Node { private: int val; Node* next; public: Node(int x, Node* next){ val = x; this->next = next; } Node(int x){ this->next = NULL; void setNext(Node* next){…} Node* getNext(){return next;} void setVal(int x){val = x;} int getVal(){return val;} };
15
Implementing LinkedList in C++
class MyLinkedList { private: Node* head; public: MyLinkedList(){head = NULL;} void addFirst(int x){head = new Node(x,head);} void add(int x){ if (head == NULL){ head = new Node(x); } else { Node *pre, *curr; pre = curr = head; while (curr != NULL){ pre = curr; curr = curr->getNext(); } pre->setNext(new Node(x)); };
16
Overloading Allow the declaration of multiple methods (constructors) of the same name but different type signatures class MyLinkedList { private Node head; public void addFirst(int x){ head = new Node(x,head); } public void addFirst(Node n){ n.setNext(head); head = n; } ….
17
Elements of OOP--Inheritance
super-class Shape Rectangle subclass or extended class Square
18
Proes and cones of inheritance
Pros Reuse of code Polymorphism Cons Inappropriate use of inheritance Cost
19
Reuse of code class Square extends Rectangle {
public Square(int x, int y, int width){ super(x,y,width,width); } public boolean isSquare(){ return true;
20
Polymorphism A variable of a class is able to reference an object of the class or its subclass Polymorphism gives flexibility Rectangle r = new Square(0,0,10); Shape[] s = new Shape[3]; S[0] = new Rectangle(0,0,10,10); S[1] = new Square(0,0,100); S[2] = new Circle(0,0,20);
21
Inappropriate use of inheritance
Use inheritance to represent has-a not is-a relationships class Stack extends LinkedList { public Stack(){…} public push(Object x){..} public pop(){…} public isEmpty(); } Stack s = new Stack(); s.add(“h”); ??? s.removeLast(); ???
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.