ObjectOriented Programming ObjectOriented Programming An object similar to
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 • 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 2
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) 3
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 4
Modification for Reuse • Modifiability of components for reuse and maintaining the independence of different components sometimes be mutually incompatible 5
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 6
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 7
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; 8 }
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. atan 2(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; 9 }
An Example public class Complex. User { public static void main(String[] args) { Complex z, w; z = new Complex (1, 2); automatic garbage binary collection is needed operation w = new Complex (-1, 1); z = z. add(w). multiply(z); is not symmetric System. out. println(z. realpart()); System. out. println(z. imaginarypart()); } } 10
An Example public class Linkable. Object { public Linkable. Object() { link = null; } public Linkable. Object(Linkable. Object link) { this. link = link; } public Linkable. Object next() { return link; } public void link. To( Linkable. Object p) { link = p; } private Linkable. Object link; } 11
An Example typedef struct Complex. Struct * Complex; no constructor struct Complex. Struct { double re, im; z = z. add(z. w); double (*realpart) (Complex this); double (*imaginarypart) (Complex this); Complex (*add) (Complex this, Complex c); Complex (*multiply) (Complex this, Complex c); }; no protection typedef struct Link. Struct * Linkable. Object; struct Link. Struct { Linkable. Object link; Linkable. Object (*next) (Linkable. Object this); void (*link. To) (Linkable. Object this, Linkable. Object link); 12 };
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 superclass public class B extends A {…} subclass 13
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 add. Front(int x) { … } public void delete. Rear() { … } } 14
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 15
An Example Queue q; Deque d; d = new Deque(); q = d; q. enqueue(2). dequeue(); q. add. Front(2); // error 16
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 to. String 17
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@73 d 6 a 5 18
An Example public class Complex { // … public boolean equals( Complex c ) { return re == c. realpart() && im == c. imaginarypart(); } public String to. String () { return re + “ + ” + im + “I”; } } Complex z = new Complex(1, 1); Complex x = new Complex(1, 1); if (x. equals(z)) System. out. println(“ok!”); System. out. println(z); // ok! // 1. 0 + 1. 0 i 19
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; } private Point center; private double radius; 20 }
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; } 21
An Example – Version 2 public abstract class Closed. Figure { public Closed. Figure (Point c) { center = c; } //. . . public abstract double area(); private Point center; } 22
An Example – Version 2 public class Circle extends Closed. Figure { public Circle( Point c, double r) { super(c); radius = r; } //. . . public double area() { return Math. PI * radius; } private double radius; } public class Rectangle extends Closed. Figure { 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; 23 }
An Example – Version 3 public abstract class Closed. Figure { public Closed. Figure (Point c) { center = c; } //. . . public abstract double area(); protected Point center; // can be accessed by subclasses } public class Circle extends Closed. Figure { public Circle( Point c, double r) { center = c; radius = r; } //. . . } 24
An Example – Version 3 Point x, y; Closed. Figure 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. 0 f = c; f. area(); // 3. 141592 … 25
Inheritance Graph • Inheritance hierarchy can be viewed as an inheritance graph Closed. Figure Rectangle Polygon Ellipse Circle Triangle Rectangle Java provides only single class inheritance Square Circle 26
Multiple Class Inheritance A B C D shared inheritance A A B C D repeated inheritance 27
An Example public class Linkable. Object { public Linkable. Object( Object d ) { link = null; item = d; } public Linkable. Object( Object d, Linkable. Object link) { this. link = link; item = d; } public Linkable. Object next() { return link; } public void link. To( Linkable. Object p) { link = p; } public Object data() { return item; } private Linkable. Object link; private Object item; } 28
An Example Linkable. Object r = new Linkable. Object(new Double(1. 2)); Linkable. Object i = new Linkable. Object(new Integer(42)); Linkable. Object c = new Linkable. Object(new Complex(1, -1)); 29
An Example public class Queue { public Queue() { rear = null; } public boolean empty() { return rear == null; } public Linkable. Object front() { return rear. next(); } public void dequeue() { if (front() == rear) rear = null; else rear. link. To(front(). next()); } public void enqueue( Linkable. Object item) { if (empty()) { rear = item; rear. link. To(item); } else { item. link. To(front()); rear. link. To(item); rear = item; } } private Linkable. Object rear; } 30
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 31
Multiple Interface Inheritance interface Linkable. Object { Linkable. Object next(); void Linkto( Linkable. Object p); } class Linkable. Complex extends Complex implements Linkable. Object { private Linkable. Object link; public Linkable. Object next() { return link; } public void link. To(Linkable. Object p) { link = p; } public Linkable. Complex(double re, double im) { super(re, im); link = null; } } 32
- Slides: 32