Download Java: Understanding Interfaces, Classes, and Exceptions in Complex Number Implementation and more Study notes Object Oriented Programming in PDF only on Docsity!
School of Computer Science
Principles of Software Construction:
Objects, Design, and Concurrency
Object-Oriented Programming in Java
Josh Bloch Charlie Garrod
Administrivia
• First homework due this Thursday, 11:59 PM
Collections usage example 3
Unfinished business from last lecture
Print index of first occurrence of each word
class Index {
public static void main(String[] args) {
Map<String, Integer> index = new TreeMap<>();
// Iterate backwards so first occurrence wins
for (int i = args.length - 1; i >= 0; i--) {
index.put(args[i], i);
System.out.println(index);
$ java Index if it is to be it is up to me to do it
{be=4, do=11, if=0, is=2, it=1, me=9, to=3, up=7}
Outline
I. Object-oriented programming basics
II. Information hiding
III. Exceptions
Classes
• Every object has a class
– A class defines methods and fields
– Methods and fields collectively known as members
• Class defines both type and implementation
– type ≈ where the object can be used
– implementation ≈ how the object does things
• Loosely speaking, the methods of a class are its
Application Programming Interface (API)
– Defines how users interact with instances
Class example – complex numbers
class Complex { private double re; // Real Part private double im; // Imaginary Part public Complex(double re, double im) { this.re = re; this.im = im; } public double realPart() { return re; } public double imaginaryPart() { return im; } public double r() { return Math.sqrt(re * re + im * im); } public double theta() { return Math.atan(im / re); } public Complex add(Complex c) { return new Complex(re + c.re, im + c.im); } public Complex subtract(Complex c) { ... } public Complex multiply(Complex c) { ... } public Complex divide(Complex c) { ... } }
Interfaces and implementations
• Multiple implementations of API can coexist
– Multiple classes can implement the same API
– They can differ in performance and behavior
• In Java, an API is specified by interface or class
– Interface provides only an API
– Class provides an API and an implementation
– A Class can implement multiple interfaces
An interface to go with our class
public interface Complex {
// No constructors, fields, or implementations!
double realPart();
double imaginaryPart();
double r();
double theta();
Complex plus(Complex c);
Complex minus(Complex c);
Complex times(Complex c);
Complex dividedBy(Complex c);
An interface defines but does not implement API
Modifying client to use interface
public class ComplexUser { public static void main(String args[]) { Complex c = new OrdinaryComplex(-1, 0); Complex d = new OrdinaryComplex(0, 1); Complex e = c.plus(d); System.out.println(e.realPart() + " + "
- e.imaginaryPart() + "i"); e = c.times(d); System.out.println(e.realPart() + " + "
- e.imaginaryPart() + "i"); } }
When you run this program, it still prints
Interface permits multiple implementations class PolarComplex implements Complex { double r; double theta; public PolarComplex(double r, double theta) { this.r = r; this.theta = theta; } public double realPart() { return r * Math.cos(theta) ; } public double imaginaryPart() { return r * Math.sin(theta) ; } public double r() { return r; } public double theta() { return theta; } public Complex plus(Complex c) { ... } // Completely different impls public Complex minus(Complex c) { ... } public Complex times(Complex c) { ... } public Complex dividedBy(Complex c) { ... } }
Why multiple implementations?
• Different performance
– Choose implementation that works best for your use
• Different behavior
– Choose implementation that does what you want
– Behavior must comply with interface spec (“contract”)
• Often performance and behavior both vary
– Provides a functionality – performance tradeoff
– Example: HashSet, TreeSet
Java interfaces and classes
• Organize program program objects types
– Each type offers a specific set of operations
– Objects are otherwise opaque
• Interfaces vs. classes
– Interface: specifies expectations
– Class: delivers on expectations (the implementation)
Check your understanding
interface Animal {
void vocalize();
class Dog implements Animal {
public void vocalize() { System.out.println("Woof!"); }
class Cow implements Animal {
public void vocalize() { moo(); }
public void moo() {System.out.println("Moo!"); }
What Happens?
1. Animal a = new Animal();
a. vocalize();
2. Dog d = new Dog();
d.vocalize();
3. Animal b = new Cow();
b.vocalize();
4. b.moo();
Historical note: simulation and the origins
of OO programming
- Simula 67 was the first object-oriented language
- Developed by Kristin
Nygaard and Ole-Johan
Dahl at the Norwegian
Computing Center
- Developed to support discrete-event simulation
- Application: operations research, e.g. traffic analysis
- Extensibility was a key quality attribute for them
- Code reuse was another