330 likes | 591 Views
Object-Oriented Programming. 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
E N D
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
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)
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
Modification for Reuse • Modifiability of componentsfor reuse and maintaining the independenceof different components sometimes be mutually incompatible
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
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
An Example public 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; }
An Example public 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; }
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
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; }
An Example typedef 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); }; no constructor z = z.add(z.w); no protection
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 { … } superclass subclass
An Example public 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() { … }}
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
An Example Queue q;Deque d;d = new Deque();q = d; q.enqueue(2).dequeue(); q.addFront(2); // error
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
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
An Example public 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
An Example – Version 1 public 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; }
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; }
An Example – Version 2 public abstract class ClosedFigure { public ClosedFigure (Point c) { center = c; } // ... public abstract double area(); private Point center; }
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; }
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; } //... }
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 …
Inheritance Graph • Inheritance hierarchy can be viewed as an inheritance graph ClosedFigure ClosedFigure Polygon Ellipse Rectangle Circle Triangle Rectangle Circle Java provides only singleclass inheritance Square
Multiple Class Inheritance A A A B C B C D D shared inheritance repeated inheritance
An Example public 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; }
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));
An Example public 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; }
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
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; } }