object oriented languages

75
1 Object Oriented Languages • Objectives: – Introduction/Overview – Class Fundamentals • classes / instances • slots and methods – Inheritance • interpretation of inheritance • code sharing • single inheritance • multiple inheritance • alternatives to multiple inheritance Ed Sykes [email protected]

Upload: winda

Post on 12-Jan-2016

49 views

Category:

Documents


3 download

DESCRIPTION

Object Oriented Languages. Ed Sykes [email protected]. Objectives: Introduction/Overview Class Fundamentals classes / instances slots and methods Inheritance interpretation of inheritance code sharing single inheritance multiple inheritance - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Object Oriented Languages

1

Object Oriented Languages• Objectives:

– Introduction/Overview– Class Fundamentals

• classes / instances• slots and methods

– Inheritance• interpretation of inheritance• code sharing• single inheritance• multiple inheritance• alternatives to multiple inheritance

Ed [email protected]

Page 2: Object Oriented Languages

2

Introduction / Overview2 main approaches to OO programming languages:

– class-based– prototype-based

Page 3: Object Oriented Languages

3

Introduction / OverviewClass-based OO programming languages:

A class is a template for the instantiation of objects:

instance == object

• classes are static source code representations• objects are dynamic entities which live during program execution. Objects are distinct entities.

Page 4: Object Oriented Languages

4

Introduction / Overview• Objects exhibit 3 main properties:

– encapsulation– inheritance– polymorphism

Page 5: Object Oriented Languages

5

Introduction / Overview• Encapsulation

– hiding how things are done within the object– this includes:

• information hiding• method hiding

Page 6: Object Oriented Languages

6

Introduction / Overview• Encapsulationeg., ATM:

properties:– name/address/phone/account_type

– balance

functionality:– withdraw()

– deposit()

– get_balance()

Page 7: Object Oriented Languages

7

Introduction / Overview• Inheritance

– “is a kind of” relation

Page 8: Object Oriented Languages

8

Introduction / Overview• Polymorphism:

– “many” “forms”– same general interface, different implementation:– 2 types of polymorphism:

• compile-time polymorphism

• run-time polymorphism (dynamic binding)

Page 9: Object Oriented Languages

9

Introduction / Overview• Polymorphism:

– run-time polymorphism (dynamic binding)– closely related to inheritance– e.g., load_vehicle(), drive_vehicle()

Page 10: Object Oriented Languages

10

Object Oriented Languages• Objectives:

– Introduction/Overview– Class Fundamentals

• classes / instances• slots and methods

– Inheritance• interpretation of inheritance• code sharing• single inheritance• multiple inheritance• alternatives to multiple inheritance

Page 11: Object Oriented Languages

11

Class FundamentalsObjectives:• class/instance difference• classes as abstract data types• information hiding (encapsulation)• internal structure of classes

Page 12: Object Oriented Languages

12

Class FundamentalsClasses are central to OO programming languages• mechanism for defining sets of objects

+ operations to manipulate them

Term “class” can be interpreted as:1) a set of objects2) a program structure or module3) a factor-like entity which creates objects4) a data type

Page 13: Object Oriented Languages

13

Class Fundamentals1) “class” as a set of objects:

A class defines properties or operations that are common to a collection of objects.

Objects are manipulated by programs and exist at runtime only.

Page 14: Object Oriented Languages

14

Class Fundamentals2) “class” as a program structure or module:

- modules encapsulate types, data (variables), and operations.- modules export some entities (operations)- modules hide their internal details- Objects are instances of modules

data

operations Outside world

Page 15: Object Oriented Languages

15

Class Fundamentals3) “class” as a factory-like entity:

- Emphasizes dynamic nature of OO languages- class is a device that can create objects- opens the way to describe objects in a parametric fashion

Dynamic unique way for instantiating objects

data

operations

class

creates

object

Page 16: Object Oriented Languages

16

Class Fundamentals4) “class” as a data type:

- classes in many OO languages are considered to be types- operations defined over that type are part of the types definition e.g.,

given a class ‘C’, and a variable v.

v : C; // variable ‘v’ is of type ‘C’

Page 17: Object Oriented Languages

17

Class FundamentalsInstances:- Can be bound, pointed to, and passed in and out of procedures first-class citizens

public class Customer{

private String _name;

private String _phone;

...

public void setName(String Name)

public String getName()

...

Bobby Boogie

(905) 234-2341

Sue MacDonald

(416) 234-7642

Class (definition)

Instances (runtime entities)

Page 18: Object Oriented Languages

18

Class FundamentalsTypically, the instance creation process allows arguments:e.g., constructors (default and overloaded):

public Customer(){ System.out.println("In default constructor"); } public Customer(String name, String phone) { this(); _name = name; _phone = phone; }

public Customer(String name, String phone, double discount){ this(name, phone); this._discount = discount; }

Page 19: Object Oriented Languages

19

Class FundamentalsSlots and Methods:-2 types:

- data slots- procedural slots (methods)

class

method

method

slots

Page 20: Object Oriented Languages

20

Class FundamentalsSlot access:-Data slots represent variable or constant information- 2 modes:

- Read-only (e.g., constant)- Read-write mode (e.g., salary slot)

Page 21: Object Oriented Languages

21

Class FundamentalsSlot visibility

Externally visible slots

class

Slots visible only within class

Page 22: Object Oriented Languages

22

Class FundamentalsSlot visibility:- C++ and Java:

- public private protected- applies to data slots and method slots

public: - can be referenced, accessed and updated anywhere in the program

private: - is only visible within the class in which it is defined.

protected: - can be referenced, accessed and updated within the class and within

all of the subclasses

Page 23: Object Oriented Languages

23

Class FundamentalsSlot visibility:- Slots may be allocated in 2 different ways:

1) the slot is allocated in each instance of the class (default)

2) the slot is allocated once and is shared by all instances of a class (class variables or static variables in C++, Smalltalk)

Page 24: Object Oriented Languages

24

Class FundamentalsExample:

public Customer(){ private static double minimum_labour = 55; // $55 per hour private String _name; private String _phone;

public static void increment_labour_price(double increase){ minimum_labour += increase; // $60.50 now. } }

Invoke by using class name or instance:

Customer.increment_labour_price(5.50);

Page 25: Object Oriented Languages

25

Object Oriented Languages• Objectives:

– Introduction/Overview– Class Fundamentals

• classes / instances• slots and methods

– Inheritance• interpretation of inheritance• code sharing• single inheritance• multiple inheritance• alternatives to multiple inheritance

Page 26: Object Oriented Languages

26

Inheritance- The definition of a new class can rely upon the existence of another

definition.

- Core concept behind OO reusability

- Supports run-time polymorphism

3 views of inheritance:

1) Inheritance is a method for code sharing

2) Logical relationship between classes (specialization/generalization)

3) Type-based account (enables subtypes to be produced given a definition of a supertype)

Page 27: Object Oriented Languages

27

Inheritance

subclass

Definitions:superclass: both the immediate superclass and all ancestors of a given classsubclass: converse of superclassdirect subclass: Van is a direct subclass of Truckindirect subclass: Van is an indirect subclass of Vehicle

superclass

Page 28: Object Oriented Languages

28

InheritanceC1: superclass

C2: subclass

m11

m12

m13

s11s12

m21

m22

s21s22s23

Page 29: Object Oriented Languages

29

InheritanceInstantiated object of type C2would appear like:

m11

m12

s11s12

s21

m13

m21

m22

s22s23

Page 30: Object Oriented Languages

30

InheritanceFor single inheritance there is 1 root class from which subclasses are derived.e.g., an inheritance graph

root

C1

C2

C3 D

B

E F

Page 31: Object Oriented Languages

31

InheritanceFrom C3, a class sees only a linear sequence of classes between it and the root classe.g., an inheritance chain

root

C1

C2

C3

Page 32: Object Oriented Languages

32

Inheritance• example:

• Car extends Vehicle:

• in Car, –inherited slots (instance variables)–methods from the Vehicle class (potentially overriden)

Page 33: Object Oriented Languages

33

Inheritancepublic class Car extends Vehicle {

...

}

public class Sports_Car extends Car {

...

}

Each class inherits ALL

variables, and methods

from parent class.

Page 34: Object Oriented Languages

34

Inheritancepublic class Vehicle {

private String _vin;

private String _make;

private String _model;

private int _year;

private long _odometer;

}

public class Car extends Vehicle {

private float _max_speed;

private int _num_of_passengers;

private int _horse_power;

private float _engine_size;

private String _air_cond;

}

Page 35: Object Oriented Languages

35

Inheritance• super()• used for calling parent class constructors

• 1st line in subclass constructor

public Sports_Car() {

super();

}

public Sports_Car(String owner, String vin, String make,

String model, int year, long odo) {

super(owner,vin,make,model,year,odo);

}

What method is called here?

what method is called here?

Page 36: Object Oriented Languages

36

Inheritance• super.method()

• Within a subclass' overriden method, invoke parent's method.public class Building {

...

public void drawBuilding() { ... } // implementation for Building

public void calcArea() { ... }

}

public class House extends Building {

...

public void calcArea() {

super.calcArea(); // invoke Building’s calcArea() method

...

}

}

Page 37: Object Oriented Languages

37

Inheritance• Adding specific methods to the subclasspublic class Building {

private int _rooms;

private int _floors;

private int _area;

public void setRooms(int num) { ... }

public int getRooms() { ... }

public void setFloors(int num) { ... }

}

public class House extends Building {

private int _bedrooms;

private int _baths;

public void setBedrooms(int num) { ... }

public int getBedrooms() { ... }

public void setBaths(int num) { ... }

}

Page 38: Object Oriented Languages

38

Abstract Classes• serves as a place holder in an inheritance graph

• defines those properties and behaviours required by many other classes

Note: 1) no objects can be created of this class type. It is

merely used as a foundation for subclasses.

2) But you can create references to this class. This is the principle way we achieve run-time polymorphism.

Page 39: Object Oriented Languages

39

Abstract Classespublic abstract class Convert {

protected double _init; // initial value

protected double _conv; // converted value

public Convert(double i) { _init = i; }

public double getconv() { return _conv; }

public double getinit() { return _init; }

public abstract void compute(); // abstract method

Abstract Method:• implementation stub• part of an abstract class• must be defined in a concrete subclass

Page 40: Object Oriented Languages

40

Abstract Classes class l_to_g extends Convert {

public l_to_g(double i) { super(i); }

public void compute() { // definition of abstract method

_conv = _init / 3.7854;

}

public String toString() {

return (this.getinit() + " litres is " + this.getconv()

“ gallons” );

}

}

Page 41: Object Oriented Languages

41

Abstract Classes

// Fahrenheit to Celsius

class f_to_c extends Convert {

public f_to_c(double i) { super(i); }

public void compute() { // definition of abstract method

_conv = (_init-32) / 1.8;

}

public String toString() {

return (this.getinit() + " Celsius is " +

this.getconv() );

}

}

Page 42: Object Oriented Languages

42

Abstract Classes public static void main(String[] args) {

Convert p; // pointer to base class

l_to_g lgob = new l_to_g(5); // litres to gallons object

p = lgob;

p.compute();

System.out.println(p);

f_to_c fcob = new f_to_c(82);

p = fcob;

p.compute();

System.out.println(p);

}

}

would Convert p = new Convert(); work? why?

Page 43: Object Oriented Languages

43

Abstract Class Eg. in C++ class number { protected:

int val: public:

void setval(int i) {val = i;}virtual void show() = 0; // declare show() to be a pure virtual function

};class hextype : public number { public:

void show() {cout << hex << val <<“\n”; }};class dectype : public number { public:

void show() {cout << val <<“\n”; }};class octtype : public number { public:

void show() {cout << oct << val <<“\n”; }};main() {

dectype d;hextype h;octtype o;d.setval(20);d.show(); // Output: 20 - decimalh.setval(20);h.show(); // Output: 14 - hexidecimalo.setval(20);o.show(); // Output: 24 - octal

}

Page 44: Object Oriented Languages

44

Inheritance as SubtypingOne problem with inheritance is that it should be hidden from its subtypes (its clients)

In other words:- the way in which a class is derived should not matter to users of that class.- The effects of inheritance can be felt when redefining subclasses

Page 45: Object Oriented Languages

45

Inheritance as Subtyping-Subtyping:

- consists of the rules by which objects of one type (class) are determined to be acceptable in contexts that expect another type (class) (e.g., downcasting)

- is important since the rules determine the legality of programs

- should be based on the behaviour of objects

Page 46: Object Oriented Languages

46

Inheritance as Subtyping-Subtyping:

- if instances of class X meet the external interface of class

Y then X should be a subtype of Y.

Y

X

Page 47: Object Oriented Languages

47

Inheritance as Subtyping- The concept of a type hierarchy can be represented by

abstract classes (or interfaces)

- behavioural subtyping cannot be deduced without formal semantic specification of behaviour.

- without these rules, subtypes can only be deduced on the basis of external interfaces of a syntactic nature (e.g., method signatures)

Page 48: Object Oriented Languages

48

Inheritance as Subtyping- The programmer should be able to specify that the class is

not a subtype of a parent or that the class is a subtype of an unrelated class (i.e., not its parent)

- The 1st case comes about when the behaviour of the objects is incompatible with the interface of parent objects.

- The 2nd case arises when the class supports the external interface of another class without sharing its implementation

Page 49: Object Oriented Languages

49

Multiple Inheritance- Natural extension of single inheritance- corresponds to the application domain in a natural fashionInheritance structure is a lattice or Directed Acyclic Graph:

B

X

C

Y

D

F

GA

HE

Searching is required since slots and methods may not be defined locally

What are the paths from X to Y?

Page 50: Object Oriented Languages

50

Multiple Inheritance- problems with multiple inheritance:

- how to search the inheritance lattice?- replication of inherited slots (“common roots” problem)- meaning of the program can be different depending on search

algorithm

B

X

C

Y

D

F

GA

HE

Page 51: Object Oriented Languages

51

Approaches to Multiple Inheritance3 approaches to finding a slot in a multiple inheritance lattice:

- Tree Inheritance- Graph Inheritance- Linearized Inheritance

Given the lattice structure:

Y2

X

Z

Y1

Page 52: Object Oriented Languages

52

Tree Inheritance- We can transform a lattice into a tree:

Y2

X

Z

Y1

-involves labeling nodes when conflicts arise-thus, a class may inherit more than 1 copy of a slot (or method) but under different names

renaming algorithm is required

Each parent of each class will define a completely separate set of inherited slots

Encapsulation is preserved: i.e., the way in which a class is derived should not be visible – only the immediate superclass should be visible to a class.…but there are problems…

Page 53: Object Oriented Languages

53

Tree Inheritance- Problems:

- Tree inheritance radically alters the semantics of inheritance:- e.g.,

Point

x ymove()

BoundedPoint HistoryPoint

Want to create a BoundedHistoryPoint…

Page 54: Object Oriented Languages

54

Tree Inheritance- Problems:

Point

x ymove()

BoundedPointx1 y1move1()

HistoryPoint

x2 y2move2()

BoundedHistoryPointx3 y3x4 y4move3()move4()

In some examples, whereexactly 1 instance of a property makes sense tree inheritance does not work it leads to duplication

Page 55: Object Oriented Languages

55

Graph InheritanceMethod: Work with the lattice directly.

Search the inheritance graph somehow and resolve conflicts in some way.

Problem: graph inheritance exposes the inheritance structure (i.e., each class has knowledge about its set of ancestors):

S2

B

A

S1

- If S1 defines those slots in A for itself, any side-effects are not seen in S2.- However, if S1 merely inherits slots from A, all side-effects will be seen in S2.

The structure of inheritance is exposed to subclasses This violates the concept of encapsulation

Page 56: Object Oriented Languages

56

Graph InheritanceSolution:

- special-purpose methods are used to resolve conflicts- e.g., redefine the inherited operation in the child class - the child can invoke the operations defined by the parent and then perform any local computation (e.g., super in Java)

S2

B

A

S1

f()

redefined f()

However, the inheritance structureis still exposed which leads to problems…

Page 57: Object Oriented Languages

57

Graph InheritanceProblem: A programmer cannot change the use of inheritance within in

a class without potentially breaking some descendent class.e.g.,

Y2

X

Z

Y1

f()

f()

If operation f() is defined only by class Z, it will be inherited by Y1 and Y2 and then by X. No problems

f()

f()

However, suppose the programmer changes Y2 so that it no longer inherits from Z but supports the samebehaviour Class X will be in error: 2 different f() methods will be inherited (one from Z via Y1, the other from class Y2)

Y2

X

Z

Y1

f()

f()

f()f’()

f’()

Page 58: Object Oriented Languages

58

Linearized Inheritance- method:

1) flatten the inheritance structure into a linear chain without duplicates2) search the chain in order to find slots (similar to single inheritance)

S2

B

A

S1

-The order in which the linearization algorithm encounters superclasses in important - Programmer can set the order

-Suppose S1 and S2 define slot “x”:-One or the other will be selected; the other will be masked off by the inheritance algorithm

Note: the choice between them is arbitrary unless more information is given.

x x

Page 59: Object Oriented Languages

59

Linearized Inheritance- problem:

- Inherited classes are not guaranteed communication with their direct ancestors.- The linearization algorithm can insert unrelated classes between an inheriting

class and one of its direct ancestors.

S2

B

A

S1 B – S1 – S2 – A

Consequence: B cannot communicate directly with S2, similarly, S1 cannot communicate with A

Page 60: Object Oriented Languages

60

Linearized Inheritance- inheritance involves search for slots (and methods)- approach:

- Linearize the graph then remove duplicates from one end or other of the resulting sequence

- Recall:

B

X

C

Y

D

F

GA

HE

Implementation: Pre-order traversal: - visit each node and put each node into a list when we visit it - once we have collected all the nodes, we can remove all duplicates

2 ways to remove duplicates: - remove duplicates from the front - remove them from the end.

Page 61: Object Oriented Languages

61

Linearized Inheritance- problems:

1) Linearization process inserts unrelated classes between related ones programmer is totally unaware… iteration over ancestors can generate

unexpected results.

2) Communication with the “real” parents is very hard to establish.

Page 62: Object Oriented Languages

62

Implemented Multiple Inheritance Techniques

- In C++ a new kind of superclass is used: virtual superclassCompiler is instructed to use only 1 copy of the named superclassOnly 1 copy will appear in any of its subclasses

C

D

A

B

Page 63: Object Oriented Languages

63

// This program will not compile

#include “iostream.h”

class base {

public:

int i;

};

class derived1 : public base {

public:

int j;

};

class derived2 : public base {

public:

int k;

};

class derived3 : public derived1, public derived2 { // This causes two copies

public: // of base in derived3

int sum;

};

Page 64: Object Oriented Languages

64

main() {

derived3 ob;

ob.i = 10; // Which i ?

ob.j = 20;

ob.k = 30;

ob.sum = ob.i + ob.j + ob.k; // which i?

cout << ob.i << “ “; // which i ?

cout << ob.j << “ “ << ob.k << “ “;

cout << ob.sum;

}

Page 65: Object Oriented Languages

65

Two ways to solve this problem:

1. Use the scope resolution operator.

e.g.,

given ob of type derived3,

ob.derived1::i = 10;

2. Using virtual base classes

Page 66: Object Oriented Languages

66

// Virtual base classes

#include “iostream.h”

class base {

public:

int i;

};

class derived1 : virtual public base { // derived1 inherits base as virtual

public:

int j;

};

class derived2 : virtual public base { // derived2 inherits base as virtual

public:

int k;

};

class derived3 : public derived1, public derived2 { // only one copy of the base class though

public:

int sum;

};

main() {

derived3 ob;

ob.i = 10; // valid here now because there is only 1 copy of the base

ob.j = 20;

ob.k = 30;

ob.sum = ob.i + ob.j + ob.k; // valid here

cout << ob.i << “ “ << ob.k << “ “; // and here too

cout << ob.sum;

}

Page 67: Object Oriented Languages

67

Alternatives to Multiple Inheritance- Interfaces in Java- Mixin classes

Page 68: Object Oriented Languages

68

Interfaces• Are:

– fully abstract classes:– ALL methods are: abstract– ALL variables are: public static final

• A class implements 1 or more interfaces• A class can extend only 1 class

Java supports the principle of multiple inheritance by using interfaces.

Page 69: Object Oriented Languages

69

Interfaces• E.g.,

Drivable:

• cars, boats, train, etc.,

(any 2D travelable vehicle/craft)

Flyable

• kite, plane, jet, etc.

(any 3D travelable object (ufo?))

Page 70: Object Oriented Languages

70

Interfacespublic interface Drivable {

int MAX_DEGREE = 45;

void turnLeft(int deg);

void turnRight(int deg);

}

public interface Flyable {

int MAX_DEGREE = 80;

void turnLeft(int deg);

void turnRight(int deg);

void turnUp(int deg);

void turnDown(int deg);

}

recall: all methods are implicitlypublic abstract

all variables are implicitlypublic static final

Page 71: Object Oriented Languages

71

Interfacespublic class Sailboat extends Boat

implements Drivable {

public void turnLeft(int deg) {

// my definition of turnLeft

}

public void turnRight(int deg) {

// my definition of turnRight

}

}

Page 72: Object Oriented Languages

72

Mixin Classes- Similar to abstract classes

- Are useful and highly flexible building blocks to construct inheritance hierarchies

- alternative approach to multiple inheritance

Page 73: Object Oriented Languages

73

Mixin Classesclass Point2D(xc: Int, yc: Int) {

val x = xc;

val y = yc;

override def toString() = "x = " + x + ", y = " + y;

}

class ColouredPoint2D(u: Int, v: Int, c: String) extends Point2D(u,v){

var color = c;

def setColour(newCol: String): Unit = colour = newCol;

override def toString() = super.toString() + ", col = " + colour;

}

Point2D

val x: Int;val y: Int;override def toString();

ColouredPoint2D

val colour: String;val setColour(c: String): Unit;override def toString();

Our mixin class

Page 74: Object Oriented Languages

74

Mixin Classesclass Point3D(xc: Int, yc: Int, zc: Int) extends Point2D(xc, yc) {

val z = zc;

override def toString() = super.toString() + ", z = " + z;

}

class ColoredPoint3D(xc: Int, yc: Int, zc: Int, col: String)

extends Point3D(xc, yc, zc)

with ColoredPoint2D(xc, yc, col);

Our mixin class

Rule: A class can only be used as a mixin in the definition of another class, if this other class extends a subclass of the superclass of the mixin.

Since ColoredPoint3D extends Point3D and Point3D extends Point2D (the superclass of ColoredPoint2D), the code is well-formed.

Page 75: Object Oriented Languages

75

Mixin ClassesPoint2D

val x: Int;val y: Int;override def toString();

ColouredPoint2D

val colour: String;val setColour(c: String): Unit;override def toString();

Point3D

val x: Int;val y: Int;override def toString();

ColouredPoint3D

“Mixed in”