ObjectOriented Programming OOP started with Simula 67 Ole

Object-Oriented Programming • OOP started with Simula 67 (Ole Dahl). The language introduced objects for use in discrete event simulation. • Simula affected various data abstraction languages (Clu, Alphard), but OOP didn’t really take off until the Xerox Dynabook project’s third revision of Smalltalk (Smalltalk 80). • OOP was to the 90’s what structured programming was to the 70’s. (Don’t even ask about the 80’s. ) • As far as I can tell, the newest paradigm reaching any level of notice is aspect-oriented programming, however, it has gotten significantly less attention than did Smalltalk and OOP in the 80’s. It will take some heavy lifting to get AOP off the ground. Lecture #11 PLP Spring 2004, UF CISE 1

Software Reuse and Encapsulation • Louden cites five basic ways a software component can be reused: – Extension of data or operations (make a dequeue/deque out of a queue) – Restriction of the data and/or operations (possible, but not likely) – Redefinition of one or more of the operations (especially in frameworks) – Abstraction/Generalization or the collection of similar operations and data from different components into a more general component (making Shape after having made Circle and Rectangle) – Polymorphization or extension of the type of data to which operations can apply (really an artifact of the language and not some activity that can be undertaken) • In addition to reuse, OOP supports hiding the internal details of a software component by hiding state and revealing it only through methods. This allows one to change the underlying representation of a class at any time. Lecture #11 PLP Spring 2004, UF CISE 2

Object Properties • Each object is characterized by three fundamental aspects: – State (represented by instance variables) – Behavior (represented by method functions) – Identity (represented by its memory location) • Louden doesn’t mention identity, however, it’s clear that two objects stored at different locations, having identical state and behavior, still represent different objects. Think of representing tennis balls. We might choose a state representation in which each 3 dot Wilson tennis ball seems to be identical, but when we simulate the hitting of a tennis ball, some of the balls don’t actually get hit. • Behavior is implemented through methods or member functions. Invoking a method, or sending an object a message causes the method to be evaluated on the arguments provided. • A class definition identifies the instance variables and method functions that are present in each instance object belonging to the class. • The class definition also identifies the visibility or accessibility of elements of the class. It implements the encapsulation. Lecture #11 PLP Spring 2004, UF CISE 3

Java Class Definition public class Complex { private double re, im; // I don’t follow Louden’s style. I always define data first! public Complex() { re = 0; im = 0; } // Constructor (default) public Complex (double realpart, double imagpart) { re = realpart; im = imagpart; } // Constructor (2 args) public double realpart() { return re; } // getter public double imaginarypart() { return im; } // getter public Complex add(Complex c) // No overloading in Java { 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. realpat()); } } Lecture #11 PLP Spring 2004, UF CISE 4

Declaring, Defining, and Using Instances Complex z, w; // z and w both // refer to NULL // objects … z = new Complex(1, 2); w = new Complex(-1, 1); x = new Complex; // construction and // assignment // z = z. add(w); z = z. add(w). multiply(z); Lecture #11 PLP Spring 2004, UF CISE 5

Pointer (vs. Value) Semantics • As you discovered, Java uses pointer semantics for objects. That is, each variable whose type is a class has as its value a pointer to an object belonging to that class. • Benefits: – Polymorphism is easy to implement (as you will see) – Storage management becomes easy (because of the work that must be done to support this view) – Recursive structures can be built without mentioning pointers or references. • Drawbacks: – Programmer relinquishes control over allocation of objects – Initialization of variables must be done at run time unless you have a very tricky implementation or are allowed to partially execute and then checkpoint a program. – Garbage collection must be employed. Lecture #11 PLP Spring 2004, UF CISE 6

Implementation of Pointer Semantics Normal Semantics (containment) x: ≈┬╚╝╟╬ y: Execute y : = x; Pointer Semantics x: y: Lecture #11 ≈┬╚╝╟╬ Execute y : = x; PLP Spring 2004, UF CISE 7

Pointer Semantics and Recursive Data Types public class Linkable. Object { private Linkable. Object link; // Direct recursion possible // due to pointer semantics // C++ would require pointer public Linkable. Object() { link = null; } public Linkable. Object(Linkable. Object link) { this. link = link; } // Disambiguation of name // necessary because of // use as a parameter. public Linkable. Object next() { return link; } public void link. To(Linkable. Object p) { link = p; } } Lecture #11 PLP Spring 2004, UF CISE 8
- Slides: 8