An Introduction to Object-Oriented Programming toad Spring 2014 - - PowerPoint PPT Presentation

an introduction to object oriented programming toad
SMART_READER_LITE
LIVE PREVIEW

An Introduction to Object-Oriented Programming toad Spring 2014 - - PowerPoint PPT Presentation

Principles of Software Construction: Objects, Design, and Concurrency An Introduction to Object-Oriented Programming toad Spring 2014 Charlie Garrod Christian Kstner School of Computer Science Learning Goals Understanding key


slide-1
SLIDE 1

toad

Spring 2014

School of Computer Science

Principles of Software Construction: Objects, Design, and Concurrency An Introduction to Object-Oriented Programming

Charlie Garrod Christian Kästner

slide-2
SLIDE 2

toad

3

15-214

Learning Goals

  • Understanding key object-oriented concepts
  • Understand the purpose of interfaces and how interfaces

can be implemented

  • Distinguish the concepts interface, class, type
  • Explain concepts to encapsulate data and behavior inside
  • bjects
  • Explain method dispatch to objects and the differences to

non-OOP languages as C

  • Understand the difference between object identity and
  • bject equality
slide-3
SLIDE 3

toad

4

15-214

Object-Oriented Programming Languages

  • C++
  • Java
  • C#
  • Smalltalk
  • Scala
  • Objective-C
  • JavaScript
  • Ruby
  • PHP5
  • Object Pascal/Delphi
  • OCaml
slide-4
SLIDE 4

toad

5

15-214

http://spectrum.ieee.org/at-work/tech-careers/the-top-10-programming-languages

  • Oct. 2011
slide-5
SLIDE 5

toad

6

15-214

This is not a Java course

but you will be writing a lot of Java code

slide-6
SLIDE 6

toad

7

15-214

int a = 010 + 3; System.out.println("A" + a);

slide-7
SLIDE 7

toad

8

15-214

int a = 010 + 3; System.out.println("A" + a);

slide-8
SLIDE 8

toad

9

15-214

Learning Java

  • Books
  • Head First Java (CMU libraries)
  • Introduction to Java Programming
  • Introduction to Programming Using Java (free online

textbook)

  • Blue Pelican Java (free online textbook)
  • Effective Java
  • Lots of resources online…
  • Java API Documentation
  • Ask on Piazza for tips
slide-9
SLIDE 9

toad

10

15-214

Concepts of Object-Oriented Languages: Overview

  • Sending messages
  • Objects and References
  • Encapsulation (Visibility)
  • Polymorphism
  • Interfaces
  • Method Dispatch
  • Object Equality
slide-10
SLIDE 10

toad

11

15-214

Sending Messages

slide-11
SLIDE 11

toad

12

15-214

Objects

  • A package of state (data) and behavior (actions)
  • Can interact with objects by sending messages
  • perform an action (e.g., move)
  • request some information (e.g., getSize)
  • Possible messages described through an interface

interface Point { int getX(); int getY(); void moveUp(int y); Point copy(); } interface IntSet { boolean contains(int element); boolean isSubsetOf( IntSet otherSet); } Point p = … int x = p.getX(); IntSet a = …; IntSet b = … boolean s = a.isSubsetOf(b);

slide-12
SLIDE 12

toad

14

15-214

Implementing Objects

(subtype polymorphism)

slide-13
SLIDE 13

toad

15

15-214

Subtype Polymorphism

  • There may be multiple implementations of an interface
  • Multiple implementations coexist in the same program
  • May not even be distinguishable
  • Every object has its own data and behavior
slide-14
SLIDE 14

toad

16

15-214

Creating Objects

interface Point { int getX(); int getY(); } Point p = new Point() { int getX() { return 3; } int getY() { return -10; } }

slide-15
SLIDE 15

toad

17

15-214

Creating Objects

interface IntSet { boolean contains(int element); boolean isSubsetOf(IntSet otherSet); } IntSet emptySet = new IntSet() { boolean contains(int element) { return false; } boolean isSubsetOf(IntSet otherSet) { return true; } }

slide-16
SLIDE 16

toad

18

15-214

Creating Objects

interface IntSet { boolean contains(int element); boolean isSubsetOf(IntSet otherSet); } IntSet threeSet = new IntSet() { boolean contains(int element) { return element == 3; } boolean isSubsetOf(IntSet otherSet) { return otherSet.contains(3); } }

slide-17
SLIDE 17

toad

19

15-214

Classes as Object Templates

interface Point { int getX(); int getY(); } class Point implements CartesianPoint { int x,y; Point(int x, int y) {this.x=x; this.y=y;} int getX() { return this.x; } int getY() { return this.y; } } Point p = new CartesianPoint(3, -10);

slide-18
SLIDE 18

toad

20

15-214

More Classes

interface Point { int getX(); int getY(); } class SkewedPoint implements Point { int x,y; SkewedPoint(int x, int y) {this.x=x + 10; this.y=y * 2;} int getX() { return this.x - 10; } int getY() { return this.y / 2; } } Point p = new SkewedPoint(3, -10);

slide-19
SLIDE 19

toad

21

15-214

Polar Points

interface Point { int getX(); int getY(); } class PolarPoint implements Point { double len, angle; PolarPoint(double len, double angle) {this.len=len; this.angle=angle;} int getX() { return this.len * cos(this.angle);} int getY() { return this.len * sin(this.angle); } double getAngle() {…} } Point p = new PolarPoint(5, .245);

slide-20
SLIDE 20

toad

22

15-214

Implementation of interfaces

  • Classes can implement one or more interfaces.
  • Semantics
  • Must provide code for all methods in the interface(s)

public class PolarPoint implements Point, IPolarPoint {…}

slide-21
SLIDE 21

toad

23

15-214

Polar Points

interface Point { int getX(); int getY(); } class PolarPoint implements Point, IPolarPoint { double len, angle; PolarPoint(double len, double angle) {this.len=len; this.angle=angle;} int getX() { return this.len * cos(this.angle);} int getY() { return this.len * sin(this.angle); } double getAngle() {…} double getLength() {… } } IPolarPoint p = new PolarPoint(5, .245); interface IPolarPoint { double getAngle() ; double getLength(); }

slide-22
SLIDE 22

toad

24

15-214

Middle Points

interface Point { int getX(); int getY(); } class MiddlePoint implements Point { Point a, b; MiddlePoint(Point a, Point b) {this.a = a; this.b = b; } int getX() { return (this.a.getX() + this.b.getX()) / 2;} int getY() { return (this.a.getY() + this.b.getY()) / 2; } } Point p = new MiddlePoint(new PolarPoint(5, .245), new CartesianPoint(3, 3));

slide-23
SLIDE 23

toad

25

15-214

Example: Points and Rectangles

interface Point { int getX(); int getY(); } … = new Rectangle() { Point origin; int width, height; Point getOrigin() { return this.origin; } int getWidth() { return this.width; } void draw() { this.drawLine(this.origin.getX(), this.origin.getY(), // first line this.origin.getX()+this.width, this.origin.getY()); … // more lines here } };

slide-24
SLIDE 24

toad

26

15-214

Points and Rectangles: Interface

interface Point { int getX(); int getY(); } interface Rectangle { Point getOrigin(); int getWidth(); int getHeight(); void draw(); }

What are possible implementations of the IRectangle interface?

slide-25
SLIDE 25

toad

27

15-214

Java interfaces and classes Object-orientation

1.

Organize program functionality around kinds of abstract “objects”

  • For each object kind, offer a specific set of operations on

the objects

  • Objects are otherwise opaque
  • Details of representation are hidden
  • “Messages to the receiving object”

2.

Distinguish interface from class

  • Interface: expectations
  • Class: delivery on expectations (the implementation)
  • Anonymous class: special Java construct to create
  • bjects without explicit classes

Point x = new Point() { /* implementation */ };

3.

Explicitly represent the taxonomy of object types

  • This is the type hierarchy (!= inheritance, more on that later)
  • A PolarPoint is a Point
slide-26
SLIDE 26

toad

28

15-214

Encapuslation

(Visibility)

slide-27
SLIDE 27

toad

29

15-214

Contracts and Clients

  • Contract of service provider and client
  • Interface specification
  • Functionality and correctness expectations
  • Performance expectations
  • Hiding of respective implementation details
  • “Focus on concepts rather than operations”

Service implementation Service interface Client environment

Hidden from service provider Hidden from service client

slide-28
SLIDE 28

toad

30

15-214

Controlling Access

  • Best practice:
  • Define an interface
  • Client may only use the messages in the interface
  • Fields not accessible from client code
  • Methods only accessible if exposed in interface
  • Classes usable as type
  • Methods in classes usable as methods in interfaces
  • Even fields directly accessable
  • Access to methods and fields in classes can be private or public
  • Private methods and fields only accessible within the class
  • Prefer programming as an interface (Variables should have

interface type, not class type)

  • Esp. whenever there are multiple implementations of a concept
  • Allows to provide different implementations later
  • Prevents dependence on implementation details

int add(PolarPoint list) { … // preferably no int add(Point list) { … // yes!

slide-29
SLIDE 29

toad

31

15-214

Interfaces and Classes both usable as Types

  • Two ways to put an object into a variable

Point p = new CartesianPoint(3,5); PolarPoint pp= new PolarPoint(5, .353); Point CartesianPoint PolarPoint Clonable

Type Class Interface Interface Class Class

slide-30
SLIDE 30

toad

32

15-214

Interfaces and Classes (Review)

class PolarPoint implements Point { double len, angle; PolarPoint(double len, double angle) {this.len=len; this.angle=angle;} int getX() { return this.len * cos(this.angle);} int getY() { return this.len * sin(this.angle); } double getAngle() { return angle; } } Point p = new PolarPoint(5, .245); p.getX(); p.getAngle(); PolarPoint pp = new PolarPoint(5, .245); pp.getX(); pp.getAngle();

slide-31
SLIDE 31

toad

33

15-214

Controlling access by client code

class Point { private int x, y; public int getX() { return this.x; } // a method; getY() is similar public Point(int px, int py) { this.x = px; this.y = py; }// constructor creating the object } class Rectangle { private Point origin; private int width, height; public Point getOrigin() { return origin; } public int getWidth() { return width; } public void draw() { drawLine(this.origin.getX(), this.origin.getY(), // first line this.origin.getX()+this.width, origin.getY()); … // more lines here } public Rectangle(Point o, int w, int h) { this.origin = o; this.width = w; this.height = h; } }

slide-32
SLIDE 32

toad

34

15-214

Hiding interior state

class Point { private int x, y; public int getX() { return x; } // a method; getY() is similar public Point(int px, int py) { x = px; y = py; } // constructor for creating the object } class Rectangle { private Point origin; private int width, height; public Point getOrigin() { return origin; } public int getWidth() { return width; } public void draw() { drawLine(origin.getX(), origin.getY(), // first line

  • rigin.getX()+width, origin.getY());

… // more lines here } public Rectangle(Point o, int w, int h) {

  • rigin = o; width = w; height = h;

} }

Some Client Code Point o = new Point(0, 10); // allocates memory, calls ctor Rectangle r = new Rectangle(o, 5, 10); r.draw(); int rightEnd = r.getOrigin().getX() + r.getWidth(); // 5 Client Code that will not work in this version Point o = new Point(0, 10); // allocates memory, calls ctor Rectangle r = new Rectangle(o, 5, 10); r.draw(); int rightEnd = r.origin.x + r.width; // trying to “look inside”

slide-33
SLIDE 33

toad

35

15-214

Hiding interior state

class Point { private int x, y; public int getX() { return x; } // a method; getY() is similar public Point(int px, int py) { x = px; y = py; } // constructor for creating the object } class Rectangle { private Point origin; private int width, height; public Point getOrigin() { return origin; } public int getWidth() { return width; } public void draw() { drawLine(origin.getX(), origin.getY(), // first line

  • rigin.getX()+width, origin.getY());

… // more lines here } public Rectangle(Point o, int w, int h) {

  • rigin = o; width = w; height = h;

} }

Discussion:

  • What are the benefits of private fields?
  • Methods can also be private – why is this useful?
slide-34
SLIDE 34

toad

36

15-214

Constructors

  • Special “Methods” to create objects
  • Same name as class, no return type
  • May initialize object during creation
  • Implicit constructor without parameters if none provided

class BPoint { int x,y; BPoint(int x, int y) {this.x=x; this.y=y;} } BPoint p = new BPoint(3, -10); class APoint { int x,y; } APoint p = new APoint(); p.x=3; p.y=-10;

slide-35
SLIDE 35

toad

37

15-214

Breaking encapsulation: instanceof and typecast

  • Java allows to inspect an object's runtime type
  • Objects always assignable to variables of supertypes ("upcast")

(this effectively throws away parts of the interface)

  • Assignment to subtype requires downcast (may fail at runtime!)

Point p = … if (p instanceof PolarPoint) { PolarPoint q = (PolarPoint) p; q.getAngle() } PolarPoint q = … Point p = q; Point p = … PolarPoint q = (PolarPoint) p;

slide-36
SLIDE 36

toad

38

15-214

Instanceof breaks encapsulation

  • Never ask for the type of an object
  • Instead, ask the object to do something (call a method of

the interface)

  • If the interface does not provide the method, maybe there

was a reason? Rethink design!

  • Instanceof and downcasts are indicators of poor design
  • They break abstractions and encapsulation
  • There are only few exceptions where instanceof is needed
  • Use polymorphism instead
  • Pure object-oriented languages do not have an instanceof
  • peration
slide-37
SLIDE 37

toad

39

15-214

Object-Oriented Programming promotes Reuse

  • Think in terms of abstractions not implementations
  • e.g., Point vs CartesianPoint
  • Abstractions can often be reused
  • Different implementations of the same interface possible,
  • e.g., reuse Rectangle but provide different Point implementation
  • Decoupling implementations
  • Hiding internals of implementations
slide-38
SLIDE 38

toad

40

15-214

Excursion: Objects vs ADTs

interface Point { int getX(); int getY(); } class CartesianPoint implements Point { … } class PolarPoint implements Point { … } Point p = … p.getX() class CartesianPoint { … } class PolarPoint { … } Object p = … if (p instanceof CartesianPoint) return ((CartesianP.)p).x; if (p instanceof PolarPoint) return ((PolarPoint)p).r*…; datatype point = CartesianP of int * int | PolarPoint of real * real fun getX point = case shape

  • f CartesianP (x, _) => x

| PolarPoint (r, a) => r*…

slide-39
SLIDE 39

toad

41

15-214

Excursion: Objects vs ADTs

  • OOP solution with polymorphism
  • Easy to extend with new

implementations of interface

  • Functions fixed; adding a function

to the interface requires changes in all implementations

interface Point { int getX(); int getY(); } class CartesianPoint implements Point { … } class PolarPoint implements Point { … } Point p = … p.getX() class CartesianPoint { … } class PolarPoint { … } Object p = … if (p instanceof CartesianPoint) return ((CartesianP.)p).x; if (p instanceof PolarPoint) return ((PolarPoint)p).r*…;

  • ADT solution with case analysis/

pattern matching

  • ADTs fixed; cannot add new class

without changing all functions

  • Easy to add new functions
  • No language/compiler support in

Java

slide-40
SLIDE 40

toad

42

15-214

Dynamic Dispatch

(subtype polymorphism)

slide-41
SLIDE 41

toad

43

15-214

(Subtype) Polymorphism

  • A type (e.g. Point) can have many forms (e.g.,

CartesianPoint, PolarPoint, …)

  • All implementations of an interface can be used

interchangeably

  • When invoking a method p.x() the specific implementation
  • f x() from object p is executed
  • The executed method depends on the actual object p, i.e., on

the runtime type

  • It does not depend on the static type, i.e., how p is declared
slide-42
SLIDE 42

toad

44

15-214

Objects and References (example)

// allocates memory, calls ctor Point o = new PolarPoint(0, 10); Rectangle r = new MyRectangle(o, 5, 10); r.draw(); int rightEnd = r.getOrigin().getX() + r.getWidth(); // 5

slide-43
SLIDE 43

toad

45

15-214

What’s really going on?

Point o = new Point(0, 10); // allocates memory, calls ctor Rectangle r = new Rectangle(o, 5, 10); r.draw(); int rightEnd = r.getOrigin().getX() + r.getWidth(); // 5

main()

  • r

rightEnd=5 Method Stack r : Rectangle

  • rigin

width = 5 height = 10 getOrigin() getWidth() draw()

  • : Point

x = 0 y = 10 getX()

slide-44
SLIDE 44

toad

46

15-214

Anatomy of a Method Call r.setX(5) The receiver, an implicit argument, called this inside the method The method name. Identifies which method to use,

  • f all the methods the receiver’s

class defines Method arguments, just like function arguments

slide-45
SLIDE 45

toad

47

15-214

Java Specifics: The keyword this refers to the “receiver”

class Point { int x, y; int getX() { return this.x; } Point(int x, int y) { this.x = x; this.y = y; } } can also be written in this way: class Point { int x, y; int getX() { return x; } Point(int px, int py) { x = px; y = py; } }

slide-46
SLIDE 46

toad

48

15-214

Static types vs dynamic types

  • Static type: how is a variable declared
  • Dynamic type: what type has the object in memory when

executing the program (we may not know until we execute the program)

Point p = createZeroPoint(); p.getX(); p.getAngle(); Point createZeroPoint() { if (new Math.Random().nextBoolean()) return new CartesianPoint(0, 0); else return new PolarPoint(0,0); }

slide-47
SLIDE 47

toad

49

15-214

Method dispatch (simplified)

Example: Point p = new PolarPoint(4, .34); p.getX(); p.getAngle();

  • Step 1 (compile time): determine what type to look in
  • Look at the static type (Point) of the receiver (p)
  • Step 2 (compile time): find the method in that type
  • Find the method in the interface/class with the right name
  • Later: there may be more than one such method

int getX();

  • Keep the method only if it is accessible
  • e.g. remove private methods
  • Error if there is no such method
slide-48
SLIDE 48

toad

50

15-214

Method dispatch (conceptually)

Example: Point p = new PolarPoint(4, .34); p.getX();

  • Step 3 (run time): Execute the method stored in the object

p : PolarPoint len = 4 angle = .34 getX() q : PolarPoint len = 5 angle = .34 getX()

slide-49
SLIDE 49

toad

51

15-214

Method dispatch (actual; simplified)

Example: Point p = new PolarPoint(4, .34); p.getX();

  • Step 3 (run time): Determine the run-time type of the

receiver

  • Look at the object in the heap and get its class
  • Step 4 (run time): Locate the method implementation to

invoke

  • Look in the class for an implementation of the method we found

statically (step 2)

  • Invoke the method

int getX() { return this.len * cos(this.angle);}

Metho d area heap Java stacks pc registe rs Native metho d stacks Runtime data area Class loader .class file Execution engine

slide-50
SLIDE 50

toad

52

15-214

Method area heap Java stacks pc registe rs Native method stacks Runtime data area Class loader .class file Execution engine

The Java Virtual Machine (sketch)

slide-51
SLIDE 51

toad

53

15-214

Method area heap Java stacks pc registers Native method stacks Runtime data area Class loader .class file Execution engine

The Java Virtual Machine (sketch)

PolarPoint getX() { … } p len = 4 angle = .34 q len = 5 angle = .34

slide-52
SLIDE 52

toad

54

15-214

Check your Understanding

  • What does this program

return?

  • Are there compile-time

problems? interface Animal { void makeSound(); } class Dog implements Animal { public void makeSound() { System.out.println("bark!"); } } class Cow implements Animal { public void makeSound() { mew(); } public void mew() {System.out.println("Mew!"); } } 1 Animal a = new Animal(); 2 a.makeSound(); 3 Dog d = new Dog(); 4 d.makeSound(); 5 Animal b = new Cow(); 6 b.makeSound(); 7 b.mew(); 8 Cow c = b; 9 c.mew();

slide-53
SLIDE 53

toad

55

15-214

Object Identity & Object Equality

slide-54
SLIDE 54

toad

56

15-214

Method area heap Java stacks pc registers Native method stacks Runtime data area Class loader .class file Execution engine

The Java Virtual Machine (sketch)

PolarPoint getX() { … } p len = 4 angle = .34 q len = 5 angle = .34 r len = 5 angle = .34

slide-55
SLIDE 55

toad

57

15-214

Object Identity vs. Object Equality

  • There are two notions of equality in most OO languages
  • Every object is created with a unique identity
  • Comparing object identity compares references

in Java: a == b, a != c

  • Object equality is domain specific
  • When are two points equal?
  • Developer needs to provide own equals functions
  • Java provides a contract for equal
  • Equals hard to implement correctly (more on this later)
  • Object identity faster to decide (comparing references

instead of calling functions)

a.equals(b), c.equals(a), d.equals(a)? Point a = new PolarPoint(1,1); // new object Point b = a; // same reference, same object Point c = new PolarPoint(1,1); // new object Point d = new PolarPoint(1,.9999999); // new object

slide-56
SLIDE 56

toad

58

15-214

Strings are weird!

  • The same object. References are the same.
  • Possibly different objects, but equivalent content
  • From the client perspective!! The actual internals might be different

String s1 = new String (“abc”); String s2 = new String (“abc”);

  • There are two string objects, s1 and s2.
  • The strings are are equivalent, but the references are different

if (s1 == s2) { same object } else { different objects } if (s1.equals(s2)) { equivalent content } else { not}

  • An interesting wrinkle: literals

String s3 = “abc”; String s4 = “abc”;

  • These are true: s3==s4. s3.equals(s2). s2 != s3.

Defined in the class String

slide-57
SLIDE 57

toad

59

15-214

How to implement equals?

  • All Java objects have "boolean equals(Object o)" method
  • by default checks for object identity only
  • Assumptions on "equals"
  • Defined as intended contract in the Java standard
  • Reflexive:

x x.equals(x)

  • Symmetric:

x,y x.equals(y) if and only if y.equals(x)

  • Transitive:

x,y,z x.equals(y) and y.equals(z) implies x.equals(z)

  • Consistent:

Invoking x.equals(y) repeatedly returns the same value unless x or y is modified

  • x.equals(null) is always false
  • always terminating and side-effect free
  • Hard to do correctly with subclassing, delegation, and
  • bjects of different classes
slide-58
SLIDE 58

toad

60

15-214

Equal points

  • Java's equals method "boolean equals(Object o)"
  • Typecast needed when comparing any specifics

class CartesianPoint { private int x, y; int getX() { return this.x; } int getY() { return this.y; } boolean equals(Object o) { if (!(o instanceof CartesianPoint)) return false; CartesianPoint that = (CartesianPoint) o; return (this.getX() == that.getX()) && (this.getY() == that.getY()); } }

slide-59
SLIDE 59

toad

61

15-214

Equal points

  • Java's equals method "boolean equals(Object o)"
  • Typecast needed when comparing any specifics

class CartesianPoint implements Point { private int x, y; int getX() { return this.x; } int getY() { return this.y; } boolean equals(Object o) { if (!(o instanceof Point)) return false; Point that = (Point) o; return (this.getX() == that.getX()) && (this.getY() == that.getY()); } }

slide-60
SLIDE 60

toad

62

15-214

One more thing: Hashcode

  • Fast search?
  • Sorting or hashing
  • Many Java libraries use hashing
  • Requires that equal (and identical) objects have the same

hash

  • Every Java object has "int hashCode()" function
  • Object provides hash, not external function
  • by default, hash based on object identity
  • Java specification: Equal objects return the same hash!
  • x.equals(y) implies x.hashCode() == y.hashCode()
  • (same hash code does not imply equality though)
  • Consistency: repeatedly calling hashCode returns the same

value

  • Whenever providing an "equals" function always provide a

corresponding "hashCode" function

slide-61
SLIDE 61

toad

63

15-214

Equal points

  • Java's equals method "boolean equals(Object o)"
  • Typecast needed when comparing any specifics

class CartesianPoint { private int x, y; int getX() { return this.x; } int getY() { return this.y; } boolean equals(Object o) { if (!(o instanceof CartesianPoint)) return false; CartesianPoint that = (CartesianPoint) o; return (this.getX() == that.getX()) && (this.getY() == that.getY()); } int hashCode() { return x + y*7; } }

slide-62
SLIDE 62

toad

64

15-214

Equal points

  • Java's equals method "boolean equals(Object o)"
  • Typecast needed when comparing any specifics

class CartesianPoint { private int x, y; int getX() { return this.x; } int getY() { return this.y; } boolean equals(Object o) { if (!(o instanceof CartesianPoint)) return false; CartesianPoint that = (CartesianPoint) o; return (this.getX() == that.getX()) && (this.getY() == that.getY()); } int hashCode() { return x + y*7; } }

slide-63
SLIDE 63

toad

65

15-214

Check your understanding

  • Complete this class to support object equality checks

class Person { private String firstName, lastName; private int ssn; Person(String name, int ssn) { this.firstName = name.split(" ")[0]; this.lastName = name.split(" ")[1]; this.ssn = ssn; } }

slide-64
SLIDE 64

toad

66

15-214

Modules

slide-65
SLIDE 65

toad

67

15-214

Module Systems

  • Many languages have a module system
  • Modules can be developed independently
  • Modules encapsulate functionality behind

interfaces, own internal name space

  • Modules can be composed (importing or linking)
  • Type errors are checked within modules

Module 1: XML Library Module 3: Other XML Library Module 2: HTML Rendering Module 4: Web Browser Module 5: Your Application

imports imports

slide-66
SLIDE 66

toad

68

15-214

Java's "module system"

  • Classes/Objects encapsulate methods and

fields

  • No module imports
  • Global name space (worldwide)
  • Avoid name clashes by naming

conventions

  • edu.cmu.cs.214.assignment1.Graph
  • Fully qualified names, typically including

domain names new edu.cmu.cs.214.assignment1.Graph(

new java.util.List(…));

slide-67
SLIDE 67

toad

69

15-214

Java's Imports

  • Imports as shorthand for not having to write fully qualified

names

  • Fully qualified names may still be used, especially if multiple

types with the same name are in scope (Compiler will warn about ambiguous references)

new edu.cmu.cs.214.assignment1.Graph(new java.util.List(…)); import edu.cmu.cs.214.assignment1.Graph; import java.util.*; new Graph(new List(…)); import java.util.*; new edu.cmu.mylist.List(new List(…));

slide-68
SLIDE 68

toad

70

15-214

Java's Packages

  • Every substring in a fully qualified name corresponds to a

package

  • Package represented with folders (IDEs offer better

abstractions)

  • In practice: Organize related classes in package
  • Packages have extra visibility mechanisms:

Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N default (no modifier) Y Y N N private Y N N N

slide-69
SLIDE 69

toad

71

15-214

Encapsulation design principles

  • Restrict accessibility as much as possible
  • Make data and methods private unless you

have a reason to make it more visible

  • Use interfaces to abstract from implementations
  • -> Easier to develop, test, understand, debug,

use, and optimize code in isolation. "The single most important factor that distinguishes a well-designed module from a poorly designed one is the degree to which the module hides its internal data and

  • ther implementation details." -- Josh Bloch
slide-70
SLIDE 70

toad

72

15-214

Class Loading

  • All classes in the class path are accessible through

imports or fully qualified names (modulo visibility)

  • .jar files contain bundled classes
  • essentially just a .zip file
  • Adding classes to class path when starting the

JVM java –cp /home/xanadu:lib/parser.jar:. Main

slide-71
SLIDE 71

toad

73

15-214

Module Systems for Java

  • Java provides deep hooks into how classes

are loaded

  • Separate module systems exist
  • OSGi commonly used, e.g., in Eclipse
  • Ongoing discussions for Java 9 (JSR 277)
  • External module systems have a separate

module construct and separate access control

  • Classes with the same (fully qualified)

name can coexist (e.g. revisions)

slide-72
SLIDE 72

toad

74

15-214

Object orientation (OO)

  • History
  • Simulation – Simula 67, first OO language
  • Interactive graphics – SmallTalk-76 (inspired by Simula)
  • Object-oriented programming (OOP)
  • Organize code bottom-up rather than top-down
  • Focus on concepts rather than operations
  • Concepts include both conventional data types (e.g. List),

and other abstractions (e.g. Window, Command, State)

  • Some benefits, informally stated
  • Easier to reuse concepts in new programs
  • Concepts map to ideas in the target domain
  • Easier to extend the program with new concepts
  • E.g. variations on old concepts
  • Easier to modify the program if a concept changes
  • Easier means the changes can be localized in the code base
slide-73
SLIDE 73

toad

75

15-214

Toad’s Take-Home Messages

  • Objects correspond to things/concepts of interest
  • An interface states expectations for an object
  • Objects embody:
  • State – held in fields, which hold or reference data
  • Actions – represented by methods, which describe operations on state
  • Constructors – how objects are created
  • A class is a template for creating objects
  • Subtype polymorphism allows different implementations of

the same interface; method selected at runtime

  • Encapsulation hides implementation internals from users
  • Object equality is different from object identity, equality and

hashcode

  • Fully qualified names, packages, and imports to structure

the name space