Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
Implications of Substitution Fall 2005 OOPD John Anthony.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
OOP Languages: Java vs C++
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Object Oriented Programming: Java Edition By: Samuel Robinson.
CSC Programming I Lecture 8 September 9, 2002.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Implications of Inheritance COMP204, Bernhard Pfahringer.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
Programming Languages and Paradigms Object-Oriented Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Implications of Inheritance COMP206, Geoff Holmes and Bernhard Pfahringer.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
COMP3190: Principle of Programming Languages
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Object-Oriented Programming Chapter Chapter
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
ISBN Object-Oriented Programming Chapter Chapter
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
ISBN Chapter 12 Support for Object-Oriented Programming.
Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { --
C11, Implications of Inheritance
Object Lifetime and Pointers
Data Types In Text: Chapter 6.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Java Primer 1: Types, Classes and Operators
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Continuing Chapter 11 Inheritance and Polymorphism
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 11: More on the Implications of Inheritance
Java Programming Language
Classes and Objects Object Creation
SPL – PS3 C++ Classes.
Presentation transcript:

Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes

Implications of Inheritance (Budd's UOOPJ, Ch. 11 ) This is a set of slides to accompany chapter 11 of Timothy Budd's textbook Understanding Object-Oriented Programming with Java Updated Edition (Addison-Wesley, 2000) Created: 14 August 2004 Revised: 11 March 2010

Idealization of is-a Relationship A TextWindow is-a Window TextWindow subclasses Window, ideally, All behavior of Window present in instances of TextWindow Variable declared as Window should be able to hold value of type TextWindow Unfortunately, practical programming language implementation issues complicate this idealized picture. 1

Impact of Inheritance on Other Language Features Support for inheritance and principle of substitutability impacts most aspects of a programming language Polymorphic variables needed  Polymorphic variable is declared as one type but actually holds value of subtype Polymorphic variables better if objects allocated on heap rather than stack – storage requirements vary among subtypes Heap allocation  Makes reference (pointer) semantics more natural than copy semantics for assignment and parameter passing copy address of object rather than value  Makes reference semantics more natural for object identity testing – compare addresses – separate operator for object value equality  Requires storage management – reference semantics makes manual allocation difficult – garbage collection encouraged 2

Polymorphic Variables Class Shape class Shape { public Shape (int ix, int iy) { x = ix; y = iy; } public String describe() { return "unknown shape"; } protected int x; protected int y; } 3

Polymorphic Variables Class Square class Square extends Shape { public Square(int ix, int iy, int is) { super(ix, iy); side = is; } public String describe() { return "square with side " + side; } protected int side; } 4

Polymorphic Variables Class Circle class Circle extends Shape { public Circle (int ix, int iy, int ir) { super(ix, iy); radius = ir; } public String describe() { return "circle with radius " + radius; } protected int radius; } 5

Polymorphic Variables Class ShapeTest class ShapeTest { public static void main(String[] args) { Shape form = new Circle(10, 10, 5); System.out.println("form is " + form.describe() ); } Output of ShapeTest: form is circle with radius 5 6

Polymorphic Variables Class CardPile from Solitaire public class CardPile {... } class SuitPile extends CardPile {... } class DeckPile extends CardPile {... } class DiscardPile extends CardPile {... } class TablePile extends CardPile {...} 7

Polymorphic Variables Class Solitaire from Solitaire Example public class Solitaire {... static public CardPile allPiles [ ];... public void init () { // first allocate the arrays allPiles = new CardPile[13];... allPiles[0] = deckPile = new DeckPile(335, 30); allPiles[1] = discardPile = new DiscardPile(268, 30); for (int i = 0; i < 4; i++) allPiles[2+i] = suitPile[i] = new SuitPile(15 + (Card.width+10) * i, 30); for (int i = 0; i < 7; i++) allPiles[6+i] = tableau[i] = new TablePile(15 + (Card.width+5) * i, Card.height + 35, i+1); } 8

Polymorphic Variables Class SolitaireFrame from Solitaire private class SolitaireFrame extends Frame {... public void paint(Graphics g) { for (int i = 0; i < 13; i++) allPiles[i].display(g); } 9

Memory Allocation in Programming Languages Static allocation Stack-based allocation Heap-based allocation 10

Stack-based Memory Allocation Memory allocated dynamically on runtime stack  Memory allocation/release tied to procedure entry/exit  Space requirement determined at compile time based on static types  Advantage: efficient all local variables allocated/deallocated as a block (activation record)  Disadvantage: polymorphic variable size not known at compile time objects stored may vary during execution 11

Heap-based Memory Allocation Memory allocated dynamically from free memory area  Memory allocation/release not tied to procedure entry/exit  Space requirement determined at run-time based using dynamic considerations – size known when allocated  Allocated objects accessed by indirection through a pointer (reference in Java)  Advantage: supports polymorphic variables values can be pointers to object on heap  Disadvantage: considered less efficient than stack- based 12

Memory Allocation in Java All object variables hold pointers to heap- allocated objects – fixed size on stack, differing sizes on heap  Variable of type Shape holds address of object on heap Polymorphic variables thus easy to implement  Assignment of Square instance to Shape variable means new address stored Primitive type variables hold values, copy on assignment, not polymorphic 13

Memory Allocation in C++ Variables stored on stack  Enough space allocated to hold instance of actual declared type  Variable of type Shape holds actual object "Ordinary" variables are not polymorphic  Assignment of Square instance to Shape variable means object copied with extra field sliced off – no longer Square Pointer variables hold addresses of objects  Support polymorphism  Assignment of Square pointer to Shape pointer variable means new address stored Objects may be allocated on heap (or on stack or statically)  Care must be taken with pointers to deallocated objects on stack (or in heap memory) 14

Copy versus Reference Semantics Copy semantics  Assignment copies entire value of right side to left-side variable  Two values are independent; changes to one do not affect the other  Examples: Java assignments to primitive variables, C++ assignments to non-pointer variables Reference (pointer) semantics  Assignment changes left-side variable to refer to right-side value  Two references same value; if value is changed, it can be observed using either reference  Examples: Java assignments to object variables C++ assignments to pointer variables. 15

Java Reference Semantics Example public class Box { public Box() { value = 0; } public void setValue(int v) { value = v; } public int getValue() { return value; } private int value; } 16

Java Reference Semantics Example Example (cont.) public class BoxTest { static public void main(String[] args) { Box x = new Box(); x.setValue(7); // sets value of x Box y = x; // assign y the same value as y y.setValue(11); // change value of y System.out.println("contents of x " + x.getValue()); System.out.println("contents of y " + y.getValue()); } After y = x, both x and y in BoxTest refer to the same object Call y.setValue(11) thus changes object referred to by both x and y Message getValue() thus returns same value (11) for both x and y 17

Creating Copies in Java If need copy, explicitly create it Box y = new Box(x.getValue()); If commonly need copy, provide copy-creating method public class Box {... public Box copy() { Box b = new Box(); b.setValue(getValue()); return b; } // return (new Box()).setValue(getValue())... } and use method when needed Box y = x.copy(); 18

Creating Copies in Java Copy constructors are sometimes useful public class Box {... public Box(Box x) { value = x.getValue()); }... } Base class Object has protected method clone() that creates bitwise copy of receiver Interface Cloneable denotes objects that can be cloned – no methods in interface, just tag  Objects needed by some API methods 19

Example Java Clones Make Box a Cloneable object Implement interface Cloneable Override method clone() (which returns type Object ) Make clone() public 20

Example Java Clones (cont.) public class Box implements Cloneable { public Box() { value = 0; } public void setValue(int v) { value = v; } public int getValue() { return value; } public Object clone() { return (new Box()).setValue(getValue()); } private int value; } 21

Example Java Clones (cont.) public class BoxTest { static public void main(String[] args) { Box x = new Box(); x.setValue(7); // sets value of x Box y = (Box) x.clone(); // assign copy of x to y y.setValue(11); // change value of y System.out.println("contents of x " + x.getValue()); System.out.println("contents of y " + y.getValue()); } Values 7 and 11, respectively, would be printed by BoxTest 22

Shallow versus Deep Copying Suppose values being held by Box objects are themselves objects of type Shape (instead of int ) Box 's clone() would not copy Shape object  Clones would both refer to same Shape object  clone() creates a shallow copy If internal Shape object also copied, then it is a deep copy  Box 's method clone() could call Shape 's clone() operation Decide whether shallow or deep copy is needed for application 23

Parameter Passing as Assignment Parameter-passing is assignment from argument to parameter Java primitive values are passed by value from argument to parameter – copy semantics  Modification of parameter just local, no effect on argument Java object variables are passed by reference from argument to parameter – reference semantics Note  Value of reference is copied from argument to parameter  Modification of parameter's internal state is change to argument 24

Parameter Passing as Assignment Example public class BoxTest { static public void main (String[] args) {Box x = new Box(); x.setValue(7); // sets value of x sneaky(x); System.out.println("contents of x " + x.getValue()); } static void sneaky(Box y) { y.setValue(11); } } Value 11 would be printed by BoxTest 25

Equality Testing Primitive Types How to test whether two values of same primitive type are equal?  Test whether their values are identical, i.e., same bits  Java: x == y What about equality of values of different primitive types?  In general, will not pass type checker unless well- accepted conversion between  Java: numeric types converted and compared, but otherwise mismatched types means inequality 26

Equality Testing Object Identity Some languages compare the values; others, compare pointers (references) Java uses pointer semantics, i.e., tests object identity Java == tests object identity Integer x = new Integer(7); Integer y = new Integer(3 + 4); if (x == y) System.out.println("equivalent") else System.out.println("not equivalent")  Output is "not equivalent" 27

Equality Testing Object Identity (cont.) Objects x and y physically distinct but same value internally Java type checker  Disallows comparison of unrelated object types with ==  Can compare if one an ancestor of other eg. Circle x = new Circle(10, 10, 5); Shape y = new Square(10, 10, 5); Shape z = new Circle(10, 10, 5); Above x == y and x == z pass type checking, but neither returns true null is of type Object ; can be compared for equality with any object 28

Equality Testing Object Value Equality Java Object class has equals ( Object ) that checks for object identity by default eg.Circle x = new Circle(10, 10, 5); Shape y = new Square(10, 10, 5); Shape z = new Circle(10, 10, 5); … if (x.equals(y)) System.out.println("equivalent"); else System.out.println("not equivalent") ;  Output is “not equivalent" 29

Equality Testing Object Value Equality (cont.) Can override equals() to get more appropriate definition class Circle extends Shape {... public boolean equals(Object arg) { return arg instanceof Circle && radius == (((Circle)arg).radius); } // more compact above than textbook example } Above c.equals(d) iff c and d are both Circles with same radius regardless of location Should override equals() if object contains other objects 30

Equality Testing Object Value Equality (cont.) Be careful with asymmetric equality comparisons Suppose override equals() in Shape class Shape {... public boolean equals(Object arg) { if (arg instanceof Shape) { Shape s = (Shape)arg; return x == s.x && y == s.y ; } else return false; } But not in subclass Square 31

Equality Testing Object Value Equality (cont.) Now consider Square s = new Square(10,10,5); Circle c = new Circle(10,10,5); if (s.equals(c)) // true, uses Shape method System.out.println("square equal to circle"); if (c.equals(s)) // false, uses Circle method System.out.println("circle equal to square"); 32

Equality Testing Changing Method Arguments For equality testing, it might useful to change types of method arguments class Shape {... public boolean equals (Shape s) {return false; } } class Circle extends Shape {... public boolean equals (Circle c) {... } } class Square extends Shape {... public boolean equals (Square sq) {... } } 33

Covariance and Contravariance Covariant  An argument or return value made more specialized  Type replaced by descendant of original type Contravariant  An argument made more general Both can destroy is-a relation, have tricky semantics Most languages forbid both  Java and C++ forbid  Eiffel supports covariance 34

Storage Deallocation Polymorphic variables lead naturally to heap-based allocation Heap-based allocation requires a storage deallocation mechanism Two approaches:  Explicit deallocation by programmer  Implicit deallocation by runtime system 35

Storage Deallocation Explicit Deallocation by Programmer Programmer must return unneeded memory to system  Examples: C++ delete, Pascal dispose Advantage: efficiency Disadvantages  Attempted use of memory not yet allocated or already freed  Multiple freeing of memory  Freeing of memory that is still needed  Memory leak – allocated memory is never released 36

Storage Deallocation Implicit Deallocation by Runtime System System detects when data unneeded, automatically recovers memory Garbage collection  Examples: Java, Smalltalk, Perl Advantage  Safety and convenience Disadvantage  Relative inefficiency / loss of programmer control 37

Acknowledgement 38 This work was supported by a grant from Acxiom Corporation titled “The Acxiom Laboratory for Software Architecture and Component Engineering (ALSACE).”