Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain...
-
Upload
hugo-fowler -
Category
Documents
-
view
246 -
download
2
Transcript of Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain...
Object-Oriented Programming
2
Object-Oriented Programming
• An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain ways to events
• An object-oriented program consists of a set of objects, which can vary dynamically, and which execute by acting and reacting to each other, in much the same way that a real-world process proceeds by the interaction of real-world objects
3
Object-Oriented Programming
• Need to reuse software components as much as possible
• Need to maintain the independence of different components (Encapsulation and Abstraction)
• Need to modify program behavior with minimum changes to existing code (Inheritance)
4
Modification for Reuse
• Extension of data and/or operations
• Restriction of data and/or operations
• Redefinition of one or more of the operations
• Abstraction, or the collection of similar operations from two different components into a new component
• Polymorphization, or the extension of the type of data that operations can apply to
5
Modification for Reuse
• Modifiability of components for reuse and maintaining the independence of different components sometimes be mutually incompatible
6
Objects
• An object occupies memory and has a (modifiable) local state represented by local variables, which are not directly accessible to other objects
• An object has a set of functions and procedures through which the local state can be accessed and changed. These are called methods
7
Classes
• Objects can be declared by creating a pattern for the local state and methods
• This pattern is called a class, and it is essentially just like a data type
• An object is said to be an instance of a class
• The local variables representing an object’s state are called instance variables
8
An Examplepublic class Complex{ public Complex() { re = 0; im = 0; } public Complex (double realpart, double imagpart) { re = realpart; im = imagpart; } public double realpart() { return re; } public double imaginarypart() { return im; } public Complex add( Complex c ) { return new Complex(re + c.realpart(), im + c.imaginarypart()); } public Complex multiply (Complex c) { return new Complex(re * c.realpart() - im * c.imaginarypart(), re * c.imaginarypart() + im * c.realpart()); } private double re, im;}
9
An Examplepublic class Complex{ public Complex() { radius = 0; angle = 0; } public Complex (double realpart, double imagpart) { radius = Math.sqrt(realpart*realpart + imagpart*imagpart); angle = Math.atan2(imagpart,realpart); } public double realpart() { return radius * Math.cos(angle); } public double imaginarypart() { return radius * Math.sin(angle); } public Complex add( Complex c ) { return new Complex(realpart() + c.realpart(), imaginarypart() + c.imaginarypart()); } public Complex multiply (Complex c) { return new Complex(realpart() * c.realpart() - imaginarypart() * c.imaginarypart(), realpart() * c.imaginarypart() + imaginarypart() * c.realpart()); } private double radius, angle;}
10
An Example
public class ComplexUser{ public static void main(String[] args) { Complex z,w; z = new Complex (1,2); w = new Complex (-1,1); z = z.add(w).multiply(z); System.out.println(z.realpart()); System.out.println(z.imaginarypart()); }}
automatic garbage collection is needed
binary operation
is not symmetric
11
An Example
public class LinkableObject{ public LinkableObject() { link = null; } public LinkableObject(LinkableObject link) { this.link = link; } public LinkableObject next() { return link; } public void linkTo( LinkableObject p) { link = p; } private LinkableObject link;}
12
An Exampletypedef struct ComplexStruct * Complex;struct ComplexStruct{ double re, im; double (*realpart) (Complex this); double (*imaginarypart) (Complex this); Complex (*add) (Complex this, Complex c); Complex (*multiply) (Complex this, Complex c);};typedef struct LinkStruct * LinkableObject;struct LinkStruct{ LinkableObject link; LinkableObject (*next) (LinkableObject this); void (*linkTo) (LinkableObject this, LinkableObject link);};
z = z.add(z.w);
no constructor
no protection
13
Inheritance
• Inheritance is the major mechanism in object-oriented languages that allows the sharing of data and operations among classes, as well as the ability to redefine these operations without modifying existing code
public class B extends A{ … }
subclass
superclass
14
An Examplepublic class Queue{ … public void enqueue(int x) { … } public void dequeue() { … } public int front() { … } public bool empty () { … }}
public class Deque extends Queue{ … public void addFront(int x) { … } public void deleteRear() { … }}
15
Subtypes
• Class definitions are also type definitions
• Subtype principle: an object of a subtype may be used anywhere an object of its supertype is legal
• The subtype principle expresses the is-a relation: If A is a subclass of B, then every object belonging to A also belonging to B, or every A “is-a” B
16
An Example
Queue q;Deque d;
d = new Deque();q = d;
q.enqueue(2).dequeue();q.addFront(2); // error
17
Class Hierarchy
• Inheritance establishes a hierarchy of classes
• At the top of Java’s class hierarchy is the class Object, which establishes behavior that is common to all of Java’s objects
• By definition in Java all classes implicitly extend class Object
• Two examples of methods in Object are equals and toString
18
An Example
String s = “Hello”;String t = new String(“Hello”);// s == t is false, but s.equals(t) is true// the default is the same as ==
Complex z = new Complex(1, 1);System.out.println(z);// the default prints the class name and an// internal index. This example prints // something like Complex@73d6a5
19
An Examplepublic class Complex{ // … public boolean equals( Complex c ) { return re == c.realpart() && im == c.imaginarypart(); }
public String toString () { return re + “ + ” + im + “I”; } }
Complex z = new Complex(1, 1);Complex x = new Complex(1, 1);if (x.equals(z)) System.out.println(“ok!”); // ok!System.out.println(z); // 1.0 + 1.0i
20
An Example – Version 1public class Point{ public Point (double x, double y) { this.x = x; this.y = y; } // ... private double x; private double y;}public class Circle{ public Circle( Point c, double r) { center = c; radius = r; } //... public double area() { return Math.PI * radius * radius; } private Point center; private double radius;}
21
An Example – Version 1
public class Rectangle{ public Rectangle (Point c, double w, double h) { center = c; width = w; height = h; } // ... public double area() { return width * height; }
private Point center; private double width; private double height;}
22
An Example – Version 2
public abstract class ClosedFigure{ public ClosedFigure (Point c) { center = c; } // ... public abstract double area();
private Point center;}
23
An Example – Version 2
public class Circle extends ClosedFigure{ public Circle( Point c, double r) { super(c); radius = r; } //... public double area() { return Math.PI * radius * radius; } private double radius;}public class Rectangle extends ClosedFigure{ public Rectangle (Point c, double w, double h) { super(c); width = w; height = h; } // ... public double area() { return width * height; } private double width; private double height;}
24
An Example – Version 3
public abstract class ClosedFigure{ public ClosedFigure (Point c) { center = c; } // ... public abstract double area(); protected Point center; // can be accessed by subclasses}
public class Circle extends ClosedFigure{ public Circle( Point c, double r) { center = c; radius = r; } //...}
25
An Example – Version 3
Point x, y;ClosedFigure f;Rectangle r;Circle c;x = new Point(0, 0);y = new Point(1, -1);r = new Rectangle(x, 1, 1);c = new Circle(y, 1);f = r;f.area(); // 1.0f = c;f.area(); // 3.141592 …
26
Inheritance Graph
• Inheritance hierarchy can be viewed as an inheritance graph
ClosedFigure
Rectangle Circle
ClosedFigure
Polygon Ellipse
Rectangle CircleTriangle
Square
Java provides only singleclass inheritance
27
Multiple Class Inheritance
A
B C
D
B C
D
A A
shared inheritance repeated inheritance
28
An Examplepublic class LinkableObject{ public LinkableObject( Object d ) { link = null; item = d; } public LinkableObject( Object d, LinkableObject link) { this.link = link; item = d; }
public LinkableObject next() { return link; }
public void linkTo( LinkableObject p) { link = p; }
public Object data() { return item; }
private LinkableObject link; private Object item;}
29
An Example
LinkableObject r = new LinkableObject(new Double(1.2));LinkableObject i = new LinkableObject(new Integer(42));LinkableObject c = new LinkableObject(new Complex(1, -1));
30
An Examplepublic class Queue{ public Queue() { rear = null; }
public boolean empty() { return rear == null; } public LinkableObject front() { return rear.next(); } public void dequeue() { if (front() == rear) rear = null; else rear.linkTo(front().next()); } public void enqueue( LinkableObject item) { if (empty()) { rear = item; rear.linkTo(item); } else { item.linkTo(front()); rear.linkTo(item); rear = item; } }
private LinkableObject rear;}
31
An Example
Queue q = new Queue();q.enqueue(r);q.enqueue(i);q.enqueue(c);q.dequeue();System.out.println(q.front().data()); // prints 42
32
Multiple Interface Inheritance
interface LinkableObject{ LinkableObject next(); void Linkto( LinkableObject p);}
class LinkableComplex extends Complex implements LinkableObject{ private LinkableObject link; public LinkableObject next() { return link; } public void linkTo(LinkableObject p) { link = p; } public LinkableComplex(double re, double im) { super(re, im); link = null; }}