Copyright S. Haridi & P. Van Roy1 Object-Oriented Programming Seif Haridi Peter Van Roy.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model From kernel to practical language (CTM 2.6) Exceptions (CTM.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
Copyright 2002 by S. Haridi and P. Van Roy1 Object-Oriented Programming Seif Haridi Peter Van Roy.
Object-Oriented PHP (1)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
C. Varela; Adapted from S. Haridi and P. Van Roy1 Object-Oriented Programming Objects, Classes and Inheritance (VRH 7.1-2) Polymorphism (VRH 6.4.3) Carlos.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
C. Varela; Adapted from S. Haridi and P. Van Roy1 Object-Oriented Programming Objects, Classes and Inheritance (VRH 7.1-4) Polymorphism (VRH 6.4.3) Carlos.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
C. Varela; Adapted from S. Haridi and P. Van Roy1 Object-Oriented Programming Inheritance (VRH 7.3-4) Object System Implementation/Java(VRH 7.6-7) Carlos.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 10 Classes Continued
C++ fundamentals.
Comparison of OO Programming Languages © Jason Voegele, 2003.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 12: Adding Functionality to Your Classes.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Chapter 8 More Object Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
An Object-Oriented Approach to Programming Logic and Design
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Programming Pillars Introduction to Object- Oriented Programming.
Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 12 Support for Object oriented Programming.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Object-Oriented Programming Chapter Chapter
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Object Oriented Programming
ISBN Object-Oriented Programming Chapter Chapter
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1/21/2016COSC , Lecture 91 Programming Language Concepts, COSC Lecture 9 Stateful programming Object-Oriented Programming.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
ISBN Chapter 12 Support for Object-Oriented Programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Final and Abstract Classes
Software Engineering Fall 2005
State, Object-Oriented Programming Explicit State, Polymorphism (CTM 6
Object-Oriented Programming Encapsulation Control/Visibility (VRH 7. 3
Object-Oriented PHP (1)
Final and Abstract Classes
State, Object-Oriented Programming Explicit State, Polymorphism (CTM 6
Presentation transcript:

Copyright S. Haridi & P. Van Roy1 Object-Oriented Programming Seif Haridi Peter Van Roy

Copyright S. Haridi & P. Van Roy2 Object-oriented programming We present a rich style in program structure based on a collection of stateful entities (abstract data types) Most popular current representatives are C++, and Java Most popular design model is UML, an object-oriented design model Principle programming techniques Relation to other models (higher-order programming, component based programming, functional) Case-study in object-oriented language (based on Mozart/Oz)

Copyright S. Haridi & P. Van Roy3 Component based programming Supports –Encapsulation –Compositionality –Instantiation

Copyright S. Haridi & P. Van Roy4 Object-oriented programming Supports –Encapsulation –Compositionality –Instantiation Plus –Inheritance

Copyright S. Haridi & P. Van Roy5 Inheritance Programs can be built in hierarchical structure from data abstractions that depend on other data abstractions (Components) Object-oriented programming (inheritance) is based on the idea that data abstractions have much in common Example, sequences (stacks, lists, queues) Object oriented programming builds data abstractions incrementally, this is done by inheritance A data abstraction can be defined to ”inherit” from another abstract datatype, have substantially the same functionality of the other abstract datatype Only the difference between a data abstraction and its ancestor has to be specified

Copyright S. Haridi & P. Van Roy6 What is object-oriented programming? OOP (Object-oriented programming) = encapsulated state + inheritance Object –An entity with unique identity that encapsulates state –State can be accessed in a controlled way from outside –The access is provided by means of methods (procedures that can directly access the internal state) Class –A specification of objects in an incremental way –Incrementality is achieved inheriting from other classes –and by specifying how its objects (instances) differ from the objects of the inherited classes

Copyright S. Haridi & P. Van Roy7 Instances (objects) Interface (what methods are available) State (attributes) procedures (methods)

Copyright S. Haridi & P. Van Roy8 Classes as complete spec of a data abstraction We start our case study elements of a class (members) –attributes (mutable instance variables) –features (stateless info about objects) –methods

Copyright S. Haridi & P. Van Roy9 Classes (syntax simple) A class is a statement class  ClassVariable  attr  AttrName1  :  AttrNameN  meth  Pattern1   Statement  end : meth  PatternN   Statement  end end

Copyright S. Haridi & P. Van Roy10 Classes (syntax simplified) A class is also a value that can be in an expression position class $ attr  AttrName1  :  AttrNamen  meth  Pattern   Statement  end : meth  Pattern   Statement  end end

Copyright S. Haridi & P. Van Roy11 Classes in Oz The class Counter has the syntactic form class Counter attr val meth browse end meth inc(Value) val + Value end meth init(Value) val := Value end end

Copyright S. Haridi & P. Van Roy12 Attributes of Classes The class Counter has the syntactic form class Counter attr val meth browse end meth inc(Value) val + Value end meth init(Value) val := Value end end val is an attribute a modifiable cell that is access by the atom val

Copyright S. Haridi & P. Van Roy13 Attributes of classes The class Counter has the syntactic form class Counter attr val meth browse end meth inc(Value) val + Value end meth init(Value) val := Value end end the attribute val is accessed by the val

Copyright S. Haridi & P. Van Roy14 Attributes of classes The class Counter has the syntactic form class Counter attr val meth browse end meth inc(Value) val + Value end meth init(Value) val := Value end end the attribute val is assigned by the operator := as val :=...

Copyright S. Haridi & P. Van Roy15 Methods of classes The class Counter has the syntactic form class Counter attr val meth browse end meth inc(Value) val + Value end meth init(Value) val := Value end end methods are statements method head is a record (tuple) pattern

Copyright S. Haridi & P. Van Roy16 Classes in Oz The class Counter has the syntactic form class Counter attr val meth browse end meth inc(Value) val + Value end meth init(Value) val := Value end end

Copyright S. Haridi & P. Van Roy17 Example The following shows how an object is created from a class using the procedure New/3, whose first argument is the class, the second is the initial method, and the result is the object. New/3 is a generic procedure for creating objects from classes. declare C = {New Counter init(0)} {C browse} {C inc(1)} {C browse}

Copyright S. Haridi & P. Van Roy18 The procedure-based approach fun {Counter} X S S = {Record.toDictionary state(val:X)} % new variable X proc {Inc inc(Value)} S.val := S.val + Value end proc {Display browse} {Browse S.val} end S proc {Init init(Value)} S.val := Value end D = o(inc:Inc browse:Display init:Init) in proc{$ M} {D.{Label M} M} end end

Copyright S. Haridi & P. Van Roy19 The procedure-based approach fun {Counter} X S S = {Record.toDictionary state(val:X)} % new variable X... D = o(inc:Inc browse:Display init:Init) in proc{$ M} {D.{Label M} M} end end fun {New Class InitialMethod} O = {Class} in {O InitialMethod} O end

Copyright S. Haridi & P. Van Roy20 Example The following shows how an object is created from a class using the procedure New/3, whose first argument is the class, the second is the initial method, and the result is the object. New/3 is a generic procedure for creating objects from classes. declare C = {New Counter init(0)} {C browse} {C inc(1)} {C browse} Object interface is as a procedure with one argument (see procedure dispatch method Chapter 8)

Copyright S. Haridi & P. Van Roy21 A class X is defined by: –class X... end Attributes are defined using the attribute-declaration part before the method-declaration part: –attr A 1... A N Then follows the method declarations, each has the form: –meth E S end The expression E evaluates to a method head, which is a record whose label is the method name. Summary

Copyright S. Haridi & P. Van Roy22 An attribute A is accessed An attribute is assigned a value using A := E A class can be defined as a value: X = class $... end Summary

Copyright S. Haridi & P. Van Roy23 Attribute Initialization Stateful (may be updated by := ) Initialized at object creation time, all instances have the initial balance = 0 class Account attr balance:0 meth … end … end In general the initial value of an attribute could be any legal value (including classes and objects)

Copyright S. Haridi & P. Van Roy24 Attribute Initialization Initialization by instance class Account attr balance meth init(X) balance := X end … end C1 = {New Account init(100)} C1 = {New Account init(50)}

Copyright S. Haridi & P. Van Roy25 Attribute Initialization Initialization by brand declare L=linux class RedHat attr ostype:L meth get(X) X end end class SuSE attr ostype:L meth get(X) X end end class Debian attr ostype:L meth get(X) X end end

Copyright S. Haridi & P. Van Roy26 Example class Queue attr front back count meth init Q in front := Q back := Q count := 0 end meth put(X) Q = X|Q back := Q count + 1 end... end

Copyright S. Haridi & P. Van Roy27 Example class Queue attr front back count meth init Q in front := Q back := Q count := 0 end meth put(X) Q = X|Q back := Q count + 1 end... end front back Q0 front back a | Q1 put(a)

Copyright S. Haridi & P. Van Roy28 Example class Queue attr front back count... meth get(?X) Q in X|Q front := Q count - 1 end meth count(X) X end... end front back a | Q1 X front back a | Q1 X

Copyright S. Haridi & P. Van Roy29 Classes as incremental specs of data abstractions Object-oriented programming allows allows us to define a class by extending existing classes Three things have to be introduced –How to express inheritance, and what does it mean? –How to access particular methods in the new class and in preexisting classes –Visibility – what part of the program can see the attributes and methods of a class The notion of delegation as a substitute for inheritance

Copyright S. Haridi & P. Van Roy30 Inheritance Inheritance should be used as a way to specialize a class while retaining the relationship between methods In this way it is a just an extension of a data abstraction The other view is inheritance is just a (lazy) way to construct new abstract data types ! No relationships are preserved general class specialized class

Copyright S. Haridi & P. Van Roy31 Inheritance class Account attr balance:0 meth transfer(Amount) balance end meth getBal(B) B end A={New Account transfer(100)}

Copyright S. Haridi & P. Van Roy32 Inheritance II Conservative extension class VerboseAccount from Account meth verboseTransfer(Amount)... end The class VerboseAccount has the methods: transfer, getBal, and verboseTransfer

Copyright S. Haridi & P. Van Roy33 Inheritance II Non-Conservative extension class AccountWithFee from VerboseAccount attr fee:5 meth transfer(Amount)... end The class AccountWithFee has the mothods: transfer, getBal, and verboseTransfer The method transfer has been redefined (overridden) with another definition

Copyright S. Haridi & P. Van Roy34 Inheritance II Non-Conservative extension class AccountWithFee from VerboseAccount attr fee:5 meth transfer(Amount)... end Account VerboseAccount AccountWithFee

Copyright S. Haridi & P. Van Roy35 Static and dynamic binding Dynamic binding Inside an object O we want to invoke a method M This is written as {self M}, and chooses the method visible in the current object (M of D) class C meth M class D a subclass of C meth M O an instance of D

Copyright S. Haridi & P. Van Roy36 Static and dynamic binding Static binding Inside an object O we want to invoke a method M in a specific (super) class This is written as C, M and chooses the method visible in the super class C (M of C) class C meth M class D a subclass of C meth M O an instance of D

Copyright S. Haridi & P. Van Roy37 Static method calls Given a class and a method head m(…), a static method-call has the following form: C, m(…) Invokes the method defined in the class argument. A static method call can only be used inside class definitions. The method call takes the current object denoted by self as implicit argument. The method m could be defined in the class C, or inherited from a super class.

Copyright S. Haridi & P. Van Roy38 Inheritance class Account attr balance:0 meth transfer(Amount) balance end meth getBal(B) B end A={New Account transfer(100)}

Copyright S. Haridi & P. Van Roy39 Inheritance II Conservative extension class VerboseAccount from Account meth verboseTransfer(Amount) { self transfer(Amount)} end The class VerboseAccount has the methods: transfer, getBal, and verboseTransfer

Copyright S. Haridi & P. Van Roy40 Inheritance II Non-Conservative extension class AccountWithFee from VerboseAccount attr fee:5 meth transfer(Amount) VerboseAccount, transfer(Amount end The class AccountWithFee has the mothods: transfer, getBal, and verboseTransfer The method transfer has been redefined (overridden) with another definition

Copyright S. Haridi & P. Van Roy41 Inheritance II Non-Conservative extension class AccountWithFee from VerboseAccount attr fee:5 meth transfer(Amount) VerboseAccount, transfer(Amount end Non-Conservative inheritance is dangerous because it might change the relationship between methods and the invariants the programmer depends on Account getBalance(B); transfer(S); getBalance(B1) => B1 = B-S

Copyright S. Haridi & P. Van Roy42 Inheritance II Non-Conservative extension class AccountWithFee from VerboseAccount attr fee:5 meth transfer(Amount) VerboseAccount, transfer(Amount end Non-Conservative inheritance is dangerous because it might change the relationship between methods and the invariants the programmer depends on AccountWithFree getBalance(B); transfer(S) iff

Copyright S. Haridi & P. Van Roy43 Inheritance III Classes may inherit from one or several classes appearing after the keyword: from. A class B is a superclass of a class A if: – B appears in the from declaration of A, or – B is a superclass of a class appearing in the from declaration of A. The methods (attributes and features) available in a class C (i.e. visible) are defined through a precedence relation on the methods that appear in the class hierarchy based on the overriding relation: –A method in a class C overrides any method, with the same label, in any super class of C.

Copyright S. Haridi & P. Van Roy44 SuperClass relation C SuperClass relation is directed and acyclic.

Copyright S. Haridi & P. Van Roy45 SuperClass relation C SuperClass relation is directed and acyclic. After striking out all overridden methods each remaining method should have a unique label and is defined only in one class in the hierarchy.

Copyright S. Haridi & P. Van Roy46 Inheritance relation C m m m A (valid hierarchy) (invalid hierarchy)

Copyright S. Haridi & P. Van Roy47 Multiple Inheritance Example class Account attr balance:0 meth transfer(Amount) balance end meth getBal(?B) B end end class Customer attr name meth init(N) name := N end end class CustomerAccount from Customer Account end A={New CustomerAccount init}

Copyright S. Haridi & P. Van Roy48 Illegal inheritance class Account attr balance meth init(Amount) balance := Amount end meth transfer(Amount) balance end meth getBal(B) B end end class Customer attr name meth init(N) name := N end end class CustomerAccount from Customer Account end There are two init methods visible for CustomerAccount This is illegal

Copyright S. Haridi & P. Van Roy49 Legal inheritance class Account attr balance meth init(Amount) balance := Amount end meth transfer(Amount) balance end meth getBal(B) B end end class Customer attr name meth init(N) name := N end end class CustomerAccount from Customer Account meth init(N A) Customer, init(N) Account, init(A) end CustomerAccount has attributes balance and name methods init, transfer and getBalance This overriding is not harmful it does not change relationships in super classes

Copyright S. Haridi & P. Van Roy50 Controlling visibility Visibility is the control given to the user to limit access to members of a class (attributes, methods and features) Each member is defined with a scope (part of program text that the member can be accessed by name) Programming languages uses words like public, private and protected to define visibility Unfortunately different languages use these keywords to define different scopes

Copyright S. Haridi & P. Van Roy51 Public and private scopes in objects A private member is one which is only visible in the object instance (it is used for implementing the object) The object instance can see all the private members in its class and its super classes A public member is visible anywhere in the program It is part of the interface of the object In Oz (and Smalltalk) attributes are private and methods are public (the default rule) In Java and C++ private has another meaning

Copyright S. Haridi & P. Van Roy52 The meaning of Private C SubC SubSubC I1I4I2I3 Instances Class Hierarchy

Copyright S. Haridi & P. Van Roy53 The meaning of Private C SubC SubSubC I1I4I2I3 Instances Class Hierarchy According to Smalltalk and Oz All private memebers in this region are visible to I3

Copyright S. Haridi & P. Van Roy54 The meaning of Private C SubC SubSubC I1I4I2I3 Instances Class Hierarchy According to C++ and Java All private memebers in this region are visible to I3

Copyright S. Haridi & P. Van Roy55 Public and private scopes in objects In Oz (and Smalltalk) attributes are private and methods are public It is possible in Oz to make a method private within a class Using a variable identifier as a method head will make the method local to the class The variable is automatically bound to a unique name class C meth A(...)... end.... end

Copyright S. Haridi & P. Van Roy56 Public and private scopes in objects In Oz (and Smalltalk) attributes are private and methods are public It is possible in Oz to make a method private within a class Using a variable identifier as a method head will make the method local to the class The variable is automatically bound to a unique name ! is an escape character, !A means escape the class scope class C meth A(...)... end.... end A local A = {NewName} in class C !A(...) meth !A(...)... end.... end

Copyright S. Haridi & P. Van Roy57 Programming techniques First class messages (higher order programming) Parameterized classes Use of multiple inheritance

Copyright S. Haridi & P. Van Roy58 Techniques of higher order programming Control abstractions class HigherOrderControl meth forAll(ListObjects M) for O in ListObjects do {O M} end end... end This technique allows messages as parameters

Copyright S. Haridi & P. Van Roy59 Techniques of higher order programming Control abstractions class HigherOrderControl... meth nil skip end meth ’|’(M Ms) {self M} {self Ms} end C = {New class $ from HigherOrderControl Counter end init(0)} {C [inc(2) browse inc(3) browse]}

Copyright S. Haridi & P. Van Roy60 Patemeterized classes Classes are values like any other value Therefore is it possible to define functions that return new classes as output fun {MakeClassAcountWithFee Fee} class $ % Fee is in context environment from Account meth init(Amount) Account, init(Amount-Fee) end Account={MakeClassAccountWithFee 100} {New Account init(1000)}

Copyright S. Haridi & P. Van Roy61 Classes as first-class values fun {MakeClassAcountWithFee Fee} class $ % Fee is in closure from Account meth init(Amount) Account,init(Amount-Fee) end Account={MakeClassAccountWithFee 100} {New Account init(1000)}

Copyright S. Haridi & P. Van Roy62 Delegation Some object systems do not use inheritance (SELF) They use a notion known as delegation Every class is an object Inheritance is implemented by forwarding messages the object cannot handle to a ”delegate” which behaves as a super-class More dynamic, inheritance can be dynamic

Copyright S. Haridi & P. Van Roy63 Delegation Account = {New class $ attr balance meth init balance := 0 end meth transfer(Amount) balance end meth getBal(B) B end end init }

Copyright S. Haridi & P. Van Roy64 Programming techniques with multiple inheritance

Copyright S. Haridi & P. Van Roy65 Delegation VerboseAccount = {New class $ from BaseObject attr delegate:Account meth verboseTransfer(Amount) B in { self transfer(Amount)} {self getBalance(B)} {Show B} end meth otherwise(M) M} end end init }

Copyright S. Haridi & P. Van Roy66 Multiple inheritance Multiple Inheritance is useful when an object has to be two different things in the same program (mixin inheritance) Example, we have graphical objects –Line, circle, etc –Composite graphical objects (groups) We use multiple inheritance to add the ability of grouping figures

Copyright S. Haridi & P. Van Roy67 Class diagrams Figure Line Circle canvas x1, x2, y1, y2 canvas x, y, r init, move(X Y), display init, move(X Y), display LinkList elem, next init, add(O), forall(P) CompositeFigure init, move(X Y), display

Copyright S. Haridi & P. Van Roy68 LinkedList Class class LinkedList attr items meth init items := nil end meth add(E) items := end meth forall(M) for O do {O M} end end

Copyright S. Haridi & P. Van Roy69 LinkedList (pure object) Java style class LinkedList attr item next meth init(item:E<=null next:N<=null) item := E next := N end meth add(E) next :={New LinkedList init(item:E end meth forall(M) then M} end then forall(M)} end end

Copyright S. Haridi & P. Van Roy70 Line class Line from Figure attr canvas x1 y1 x2 y2 meth init(Can X1 Y1 X2 Y2) canvas := Can x1 := X1 y1 := Y1 x2 := X2 y2 := Y2 end meth move(X Y) x1 y1 x2 y2 end meth end

Copyright S. Haridi & P. Van Roy71 CompositeFigure class CompositeFigure from Figure LinkedList meth init LinkedList, init end meth move(X Y) {self forall(move(X Y))} end meth display {self forall(display)} end

Copyright S. Haridi & P. Van Roy72 Use of single inheritance LinkList elem, next init, add(O), forall(P) CompositeFigure init, move(X Y), display, add(O) Figure linkList 1 1 association

Copyright S. Haridi & P. Van Roy73 CompositeFigure class CompositeFigure from Figure attr linkList meth init figlist  {New LinkedList init} end meth add(F) add(F)} end meth move(X Y) forall(move(X Y))} end meth display forall(display)} end

Copyright S. Haridi & P. Van Roy74 Multiple vs. Single inheritance With multiple inheritance a composite figure is also a linked list. In general use multiple inheritence in this case if you want all operations of linked list to be available With single inheritance a composite figure completely hides the linked list Use single inheritance if you want to hide the linked list functionality

Copyright S. Haridi & P. Van Roy75 Rules for using inheritance Do not violate the substitution property. Programs working on objects of a given class should work on all the objects of its subclasses Do not use subclassing to fix small problem. That is to say do not patch up a class by making a subclass. The class hierarchy tends to get large and the program slower

Copyright S. Haridi & P. Van Roy76 When does multiple inheritance work? Multiple inheritance works well when combining two completely independent abstractions Multiple inheritance does not work when abstractions have something in common Mutiple inheritance does not work when their is a shared class with mutable attributes

Copyright S. Haridi & P. Van Roy77 When it does not work? Mutiple inheritance does not work when there is a shared class with mutable attributes Creating a BHistoryPoint object could replicate the operations on Point though HistroyPoint and BoundedPoint Known as the implementation sharing problem Point HistoryPointBoundedPoint BHistoryPoint

Copyright S. Haridi & P. Van Roy78 HOP vs.OOP We show how to get some of the flexibility of higher order programming in OOP proc {NewSort Order ?SortRoutine} proc {SortRoutine InL ?OutL}... {Order X Y Z} end class SortRoutineClass attr ord meth init(Order) ord  Order end meth sort(InL ?OutL)... order(X Y Z)} end

Copyright S. Haridi & P. Van Roy79 HOP vs.OOP We show how to get some of the flexibility of higher order programming in OOP X... Y P = proc{$} Some Statement with free X Y end.... {P} class Proc attr x y meth init(X Y) x  X y  Y end meth apply Some statement end X... Y P = {New Proc init(X Y)}.... {P apply}

Copyright S. Haridi & P. Van Roy80 HOP vs.OOP We show how to get some of the flexibility of higher order programming in OOP A lot of the higher order functionality can be coded proc {Map Xs P Ys}.... {P X Y} Ys = Y|Yr {Map Xr P Yr} meth map(Xs O Ys).... {O apply(X Y)} Ys = Y|Yr map(Xr O Yr)