Implications of Inheritance COMP204, Bernhard Pfahringer.

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Advertisements

Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Run-Time Storage Organization
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
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.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
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.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Runtime Environments Compiler Construction Chapter 7.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
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.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Advanced Java Programming CS 537 – Data Structures and Algorithms.
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.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
COMP3190: Principle of Programming Languages
Session 22 Chapter 11: Implications of Inheritance.
ISBN Object-Oriented Programming Chapter Chapter
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
CSE 332: Memory management with C++ classes Memory Management with Classes Review: for non-static built-in (native) types –default constructor and destructor.
Session 6 Comments on Lab 3 & Implications of Inheritance.
CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
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.
Implementing Subprograms
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Lecture 12 Implementation Issues with Polymorphism.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Chapter 10 Chapter 10 Implementing Subprograms. Implementing Subprograms  The subprogram call and return operations are together called subprogram linkage.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Dynamic Allocation in C
C11, Implications of Inheritance
Object Lifetime and Pointers
Memory Management with Classes
Chapter 8 Classes and Objects
Continuing Chapter 11 Inheritance and Polymorphism
CSE 303 Concepts and Tools for Software Development
Chapter 11: More on the Implications of Inheritance
Dynamic Memory Management
Java Programming Language
Dynamic Memory Management
Classes and Objects Object Creation
Chapter 10 Def: The subprogram call and return operations of
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Implications of Inheritance COMP204, Bernhard Pfahringer

Implications OO language must support polymorphic variables Polymorphic variables: memory requirements unknown at compile-time => allocated on the heap Heap allocation => reference semantics for assignment and parameter parsing Also: equality testing == object identity And: memory management necessary => garbage collection (GC)

The Polymorphic variable Square side describe() Shape x,y describe() Circle radius describe() … Shape form = new Circle(10,10,5); form.describe(); form = new Square(15,20,10); form.describe(); …

Memory Layout Stack-based vs. heap-based Stack-based tied to method entry/exit: Allocate space for all local vars on entry Deallocate on exit Access by numeric offset (activation record) Need to know space requirements at compile-time, not possible for polymorphic variables! Java: all objects allocated on the heap (new operator), local vars point to these objects, pointers have a fixed size Java has no pointers, but internally all object values are represented by pointers.

Factorial example static public int factorial(int n) { int c = n-1; int r; if (c>0) r = n*factorial(c); else r = 1; return r; } 0 n:1 4 r:1 8 c:0 0 n:2 4 r:? 8 c:1 0 n:3 4 r:? 8 c:2 third activation record second activation record first activation record Factorial(3)

Alternative approach (e.g. C++) Different approaches are possible C++: want to use stack (efficiency) Assignment: extra fields are sliced off, E.g. a Square is turned into a Shape, Need to distinguish between virtual/non-virtual methods Need destructors, Pointers/references/values Copy-semantics, …

Assignment: reference semantics Box x = new Box(); x.setValue(7); Box y = x; y.setValue(11); System.out.println(x.getValue()); System.out.println(y.getValue()); … 11 x a box y Simple copy pointer value, i.e. point to same heap-location!

Clones If copy desired, must say so, DIY approach: Box y = new Box(); y.setValue(x.getValue()); If common, package into proper method: public Box copy() { Box b = new Box(); b.setValue(getValue()); return b; }

Clones: Java Class Object provides protected method clone() creating a bitwise copy of the receiver, plus interface Cloneable Programmer must override clone() to public and implement Cloneable: public class Box implements Cloneable { … public Object clone() { Box b = new Box(); b.setValue(getValue()); return b; }} Use: Box y = (Box) x.clone(); // must cast !!!!

Clones: caveats Just a shallow copy, sometimes need deep copies x a box a shape y a box x a box a shape y a box a shape SHALLOW DEEP

Parameter passing as assignment Passing a variable considered similar to assignment, as same value accessible through two different names; pass value, loose some control: static void sneaky (Box b) {b.setValue(11);} … x.setValue(7); sneaky(x); System.out.println(x.getValue()); 11

Equality test: object identity Easy for primitives: 7 == (3+4) ‘a’ == ‘\141’ 2 == 2.0 For objects: == implements object identity, therefore: new Integer(7) != new Integer(3+4) If we really want object equality instead of object identidy: equals method

Equality test: object equality Supplied by class Object, can be overridden: class Circle extends Shape { … public boolean equals(Object arg) { return ((arg instanceof Circle) && (radius == ((Circle) arg).radius)); }} Careful: must be symmetric and transitive Heuristic: use == for numbers and null, equals in all other situations

Equality test: bug example Suppose: class Shape { … public boolean equals(Object arg) { return ((arg instanceof Shape) && (x == ((Shape) arg).x) && (y == ((Shape) arg).y)) ; }} And no equals defined in class Square, then: Square s = new Square(10,10,5); Circle c = new Circle(10,10,5); s.equals(c); // succeeds c.equals(s); // fails

Better solution class Shape { … public boolean equals(Object arg) { return ((arg.getClass() == Shape.class) && (x == ((Shape) arg).x) && (y == ((Shape) arg).y)) ; }} class Circle extends Shape { … public boolean equals(Object arg) { return ((arg.getClass() == Circle.class) && (x == ((Shape) arg).x) && (y == ((Shape) arg).y) && (radius == ((Circle) arg).radius)); }} correct, but no inheritance, code duplication

Garbage collection (GC) Heap-based memory not automatically recovered on method exit (unlike stack) Manual management error-prone (like “free” operator in C++): Forget to free: memory leaks Free multiple times Access freed memory Java compromise: have automatic garbage collection: some runtime cost, but not too bad)