6-1 © 2004, d.a. watt, university of glasgow 6 data abstraction packages and encapsulation. ...

40
6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction Packages and encapsulation. Abstract types. Classes, subclasses, and inheritance. Implementation notes.

Upload: agatha-walters

Post on 01-Jan-2016

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-1© 2004, D.A. Watt, University of Glasgow

6Data Abstraction

Packages and encapsulation.

Abstract types.

Classes, subclasses, and inheritance.

Implementation notes.

Page 2: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-2

Packages

A package is a named group of components declared for a common purpose.

These components may be types, constants, variables, procedures, etc.

Page 3: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-3

Example: Ada simple package

Package:

package Earth is

type Continent is (Africa, Asia, Australia,Europe, N_America, S_America);

radius: constant Float := 6.4e3; -- km

population: array (Continent) of Natural;

end Earth;

Bindings produced by this package:

{ Continent the type Continent = {Africa, Asia, …}, radius the real number 6.4103, population an array variable of type Continent Natural}

Page 4: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-4

Encapsulation (1)

Encapsulation means making some components of a program unit (package, abstract type, or class) private.

Several possible levels of privacy:• A component is private if it is visible only inside the program

unit.

• A component is protected if it is visible only inside the program unit and certain closely-related program units.

• A component is public if it is visible to application code outside the program unit.

A program unit’s API (application program interface) consists of its public bindings only.

Page 5: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-5

Encapsulation (2)

An Ada package consists of two parts:• Public components are declared in the package specification.

• Private components are declared in the package body (or in the private part of the package specification).

Page 6: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-6

Example: Ada package with encapsulation (1)

Package specification:

package Trig is

function sin (x: Float) return Float;

function cos (x: Float) return Float;

end Trig;public function

public function

Page 7: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-7

Example: Ada package with encapsulation (2)

Package body:

package Trig is

twice_pi: constant Float := 6.2832;

function norm (x: Float) return Float is

… -- compute x modulo twice_pi

function sin (x: Float) return Float is

… -- compute the sine of norm(x)

function cos (x: Float) return Float is

… -- compute the cosine of norm(x)

end Trig;

private constant

private function

Page 8: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-8

Example: Ada package with encapsulation (3)

This package’s API:

{ sin function procedure that approximates the sine function, cos function procedure that approximates the cosine function}

Page 9: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-9

Example: Ada encapsulated variables (1)

Package specification:

package The_Dictionary is

procedure clear;-- Make the dictionary empty.

procedure add (w: in Word);-- Add w to the dictionary if it is not already there.

function contains (w: Word)return Boolean;

-- Return true if and only if w is in the dictionary.

end The_Dictionary;

public function

public procedure

public procedure

Page 10: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-10

Example: Ada encapsulated variables (2)

Package body:

package body The_Dictionary is

cap: constant := 1000;

size: Integer := 0;words: array (1 .. cap) of Word;

procedure clear is…

procedure add (wd: in Word) is…

function contains (wd: Word)return Boolean is

end The_Dictionary;

private constant

private variablesprivate variables

access size and wordsaccess size and wordsaccess size and words

Page 11: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-11

Example: Ada encapsulated variables (3)

This package’s API:

{ clear proper procedure that makes the dictionary empty, add proper procedure that adds a word to the dictionary, contains function procedure that tests whether a word is

in the dictionary }

Possible application code:

use The_Dictionary;…if not contains(current_word) then

…add(current_word);

end if;…size := 0; illegal!

Page 12: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-12

Abstract types

An abstract type has a private representation but is equipped with public operations. (The operations may be constants and/or procedures.)

Some PLs support abstract types directly.

Ada supports abstract types by means of packages. The abstract type is declared in the package specification in two stages:• The type declaration is public, but doesn’t define the

representation.

• The private part contains a full type definition.

Page 13: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-13

Example: Ada abstract type (1)

Package specification:

package Dictionaries is

type Dictionary is limited private;-- A Dictionary value represents a set of words.

procedure clear (d: in out Dictionary);-- Make d empty.

procedure add (d: in out Dictionary;w: in Word);

-- Add w to d if it is not already there.

function contains (d: Dictionary;w: Word) return Boolean;

-- Return true if and only if w is in d.

private type

public function

public procedure

public procedure

Page 14: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-14

Example: Ada abstract type (2)

Package specification (continued):

private

cap: constant Integer := 1000;

type Dictionary isrecord

size: Integer;words: array (1 .. cap) of

Word;end record;

end Dictionaries;

private constant

full type definition

Page 15: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-15

Example: Ada abstract type (3)

Package body:

package body Dictionaries is

procedure clear (d: in out Dictionary) is

procedure add (d: in out Dictionary;w: in Word) is

function contains (d: Dictionary;w: Word)return Boolean

is…

end Dictionaries;

Page 16: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-16

Example: Ada abstract type (4)

This package’s API:

{ Dictionary an abstract type representing a set of words, clear proper procedure that makes a dictionary empty, add proper procedure that adds a word to a dictionary, contains function procedure that tests whether a word is

in a dictionary}

Page 17: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-17

Example: Ada abstract type (5)

Possible application code:

use Dictionaries;main_dict, user_dict: Dictionary;…if not contains(mainDict, current_word)

and not contains(userDict, current_word) then

…add(userDict, current_word);

end if;…user_dict.size := 0;

illegal

Page 18: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-18

Classes (1)

An object is a tuple of variable components (instance variables), equipped with a group of operations that access these variables.

A class is a set of similar objects. All objects of a given class have similar instance variables, and are equipped with the same operations.

A constructor is an operation that creates and initializes a new object of the class.

A method is an operation that inspects and/or updates an existing object of the class.

Page 19: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-19

Classes (2)

A Java class declaration:• declares all instance variables

• defines all constructors

• defines all methods

• specifies whether each of these is private, protected, or public.

A Java method call has the form “O.M(…)”:• The expression O determines the target object.

• M is the name of the method to be called.

• The call executes the method body, with this denoting the target object.

Page 20: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-20

Example: Java class (1)

Class declaration:

class Dictionary {

private int size;private String[] words;

public Dictionary (int capacity){ … }

public void add (String w) {if (! this.contains(w))

this.words[this.size++] = w;}

public boolean contains (String w){ … }

}

public method

public method

public constructor

private variablesprivate variables

Page 21: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-21

Example: Java class (2)

Possible application code:

Dictionary mainDict = new Dictionary(10000);Dictionary userDict = new Dictionary(1000);…if (! mainDict.contains(currentWord)

&& ! userDict.contains(currentWord)) {

…userDict.add(currentWord);

}…userDict.size = 0;

target object

illegal!

Page 22: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-22

Subclasses

Recall: A class C is a set of similar objects. All objects of class C have similar instance variables, and are equipped with the same operations.

A subclass of C, S, is a set of objects that are similar to one another but richer than the objects of class C:• An object of class S has all the instance variables of an object of

class C, but may have extra instance variables.

• Likewise, an object of class S is equipped with all the methods of class C, but may be equipped with extra methods.

If S is a subclass of C, we say that C is a superclass of S.

Page 23: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-23

Inheritance

A subclass is said to inherit its superclass’s instance variables and methods.

Alternatively, a subclass may override some of its superclass’s methods, by providing more specialized versions of these methods.

Page 24: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-24

Example: Java class and subclass (1)

Class declaration:

class Point {

protected double x, y;

public Point (){ x = 0.0; y = 0.0; }

public double distance (){ return Math.sqrt(x*x + y*y); }

public final void move (double dx,double dy)

{ x += dx; y += dy; }

public void draw (){ … } // draw this point on the screen

}

This method may not be overridden.

method (1)

method (2)

method (3)

Page 25: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-25

Example: Java class and subclasses (2)

Subclass declaration:

class Circle extends Point {

private double r;

public Circle (double radius){ x = 0.0; y = 0.0; r = radius; }

public void draw (){ … } // draw this circle on the screen

public double diameter (){ return 2.0*r; }

}

method (4)

method (5)

Page 26: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-26

Example: Java class and subclass (3)

Possible application code:

Point p = new Point();Circle c =

new Circle(10.0);p.move(12.0, 5.0);c.move(3.0, 4.0);… p.distance() …… c.distance() …… c.diameter() …p.draw();c.draw();

p = c;p.draw();

c is centred at (0, 0)

now p is at (12, 5)

now c is centred at (3, 4)

yields 13

yields 5

draws a point at (12, 5)

draws a circle centred at (3, 4)

yields 10

p is at (0, 0)

ditto! (dynamic dispatch)

Page 27: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-27

Example: Java class and subclasses (4)

Another subclass declaration:

class Rectangle extends Point {

private double w, h;

public Rectangle (…){ … }

public void draw (){ … } // draw this rectangle on the screen

public double width (){ return w; }

public double height (){ return h; }

}

method (6)

method (7)

method (8)

Page 28: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-28

Overriding

Each method of a class C is inherited by the subclass S, unless it is overridden by S.

The overriding method in class S has the same name and type as the original method in class C.

Some OO PLs allow programmers to specify whether a method may be overridden or not:• In C++, a method specified as virtual may be overridden.

• In Java, a method specified as final may not be overridden.

Page 29: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-29

Dynamic dispatch

If methods are overridden, and if the PL allows a variable of a particular class to refer to an object of a subclass, then method calls entail dynamic dispatch.

Consider the Java method call “O.M(E1, …, En)”:

• The compiler infers the type of O, say class C.

• The compiler checks that class C is equipped with a method named M, of the appropriate type.

• Nevertheless, it might turn out (at run-time) that the target object is actually of class S, a subclass of C.

• If method M is overridden by any subclass of C, a run-time tag test is needed to determine the actual class of the target object, and hence which of the methods named M is to be called.

Page 30: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-30

Single inheritance

Single inheritance allows each class to have at most one superclass.

Single inheritance gives rise to a hierarchy of classes.

Single inheritance is supported by most OO PLs, including Java and Ada95.

Page 31: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-31

Example: Java single inheritance

Declared classes:• Date (subclass of Object)

• Point (subclass of Object)

• Circle, Rectangle(both subclasses of Point).

Hierarchy of classes:

cloneequals…

Object

Datey, m, d

drawwidthheight

Rectanglew, h

drawdiameter

Circler

distancemovedraw

Pointx, y

Page 32: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-32

Multiple inheritance

Multiple inheritance allows each class to have any number of superclasses.

Multiple inheritance gives rise to both conceptual and implementation problems.

Multiple inheritance is supported by C++.

Page 33: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-33

Example: multiple inheritance (1)

Declared classes:• Animal• Mammal, Flier, Bird

(all subclasses of Animal)• Cat (subclass of Mammal)• Bat (subclass of Mammal

and Flier)• etc.

Class relationships: …

Mammalgestation

Flierwing-span

Birdegg-size

Cat…

Batsonar

Animalweightspeed

Eagle…

Penguin…

Page 34: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-34

Example: multiple inheritance (2)

Suppose:• the Animal class defines a method named move

• the Mammal and Flier classes both override that method.

Then which method does the Bat class inherit? E.g.:

Bat b;b.move(…); Which method does this call?

Possible answers:• Call the Mammal method (since Mammal is the first-named superclass of Bat).

• Force the programmer to choose, either in the Bat class declaration or in the method call.

• Prohibit this method call (since it is ambiguous).

Page 35: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-35

Representation of objects (1)

Each object of a given class is represented by a tag field juxtaposed with the object’s instance variables. The tag field indicates the object’s class.

Tag fields are needed whenever objects of different classes can be used interchangeably, in particular to implement dynamic dispatch.

Page 36: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-36

Representation of objects (2)

Implementation of access to an instance variable:• Let o be an object of class C.

• Each instance variable o.v has a fixed offset (determined by the compiler) relative to the base address of o.

Assuming single inheritance, this implementation works even if o is replaced by an object of a subclass of C (since inherited instance variables are located at the same offset in both C and its subclasses).

But multiple inheritance makes it more difficult to represent objects such that instance variables can be accessed efficiently.

Page 37: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-37

Example: representation of Java objects (1)

Representation of Point, Circle, Rectangle objects (simplified):

tag

x1.0

2.0 y

Point

x0.0

0.0 y

r5.0

tagCircle

x1.5

2.0 y

w3.0

4.0 h

tagRect.

The class tags are actually pointers to “class objects” (see later).

Page 38: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-38

Implementation of method calls (1)

In a method call, the target object’s address is passed to the method, along with the ordinary arguments. (Thus the method can inspect/update the target object.)

If the named method is never overridden, the method call is implemented like an ordinary procedure call. Otherwise dynamic dispatch must be implemented, as follows.

For each class C in the program, create a class object containing the addresses of C’s methods.

Make each object of class C contain (in its tag field) a pointer to C’s class object.

Page 39: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-39

Implementation of method calls (2)

Implement the method call “O.M(…)” as follows:

1. Determine the target object from O.

2. Follow the pointer from the target object’s tag field to the corresponding class object.

3. Select the method named M in the class object.

4. Call that method, passing the target object’s address along with the ordinary arguments.

The method named M can be selected efficiently in step 3, since the compiler can determine its offset relative to the base of the class object, assuming single inheritance.

Multiple inheritance makes it more difficult to implement dynamic dispatch efficiently.

Page 40: 6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance

6-40

Example: representation of Java objects (2)

Representation of Point, Circle, Rectangle objects:

tag

x1.0

2.0 y

x0.0

0.0 y

r5.0

tag

x1.5

2.0 y

w3.0

4.0 h

tag

method (1)

method (2)

method (6)

Rectangle class object

distance

move

draw

method (7) width

method (8) height

method (1)

method (2)

method (4)

Circle class object

move

draw

method (5) diameter

distancemethod (1)

method (2)

method (3)

move

draw

Point class object

distance