ObjectOriented Programming OOP ADT Abstract Data Types Inheritence
Object-Oriented Programming OOP = ADT (Abstract Data Types) + Inheritence + Dynamic binding
Abstract Data Types (ADT) 4 ADT = (V, O) – V: a set of variables – O: a set of operations 4 Advantages – Abstraction of both data and operations – Information hiding and data protection – Modularity
An example ADT: Rectangle Variables: int x, y, width, height Operations: Rectangle(int, int) void move(int, int) void resize(float) int area() boolean is. Square() print()
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); }. . . }
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); }… };
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
Using Rectangle in Java class Test. Rectangle { public static void main(String[] args){ Rectangle r = new Rectangle(0, 0, 100, 50); r. resize(1. 5); r. move(100, 100); r. print(); } }
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(); }
Implementing Linked. List 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 set. Next(Node next){…} public Node get. Next(){…} public void set. Val(int x){…} public int get. Val(){…} }
Implementing Linked. List in Java class My. Linked. List { private Node head; public void add. First(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. get. Next(); } pre. set. Next(new Node(x)); }
Implementing Linked. List in C++ class Node { private: int val; Node* next; public: Node(int x, Node* next){ val = x; this->next = next; } Node(int x){ val = x; this->next = NULL; } void set. Next(Node* next){…} Node* get. Next(){return next; } void set. Val(int x){val = x; } int get. Val(){return val; } };
Implementing Linked. List in C++ class My. Linked. List { private: Node* head; public: My. Linked. List(){head = NULL; } void add. First(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->get. Next(); } pre->set. Next(new Node(x)); } }
Overloading 4 Allow the declaration of multiple methods (constructors) of the same name but different type signatures class My. Linked. List { private Node head; public void add. First(int x){ head = new Node(x, head); } public void add. First(Node n){ n. set. Next(head); head = n; } …. }
Elements of OOP--Inheritance super-class Shape Rectangle subclass or extended class Square
Proes and cones of inheritance 4 Pros – Reuse of code – Polymorphism 4 Cons – Inappropriate use of inheritance – Cost
Reuse of code class Square extends Rectangle { public Square(int x, int y, int width){ super(x, y, width); } public boolean is. Square(){ return true; } }
Polymorphism 4 A variable of a class is able to reference an object of the class or its subclass Rectangle r = new Square(0, 0, 10); 4 Polymorphism gives flexibility Shape[] s = new Shape[3]; S[0] = new Rectangle(0, 0, 10); S[1] = new Square(0, 0, 100); S[2] = new Circle(0, 0, 20);
Inappropriate use of inheritance 4 Use inheritance to represent has-a not is-a relationships class Stack extends Linked. List { public Stack(){…} public push(Object x){. . } public pop(){…} public is. Empty(); } Stack s = new Stack(); s. add(“h”); ? ? ? s. remove. Last(); ? ? ?
- Slides: 21