C11, Implications of Inheritance

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

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.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Chapter 7:: Data Types Programming Language Pragmatics
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.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
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.
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.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Implications of Inheritance COMP204, Bernhard Pfahringer.
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.
Storage Management. The stack and the heap Dynamic storage allocation refers to allocating space for variables at run time Most modern languages support.
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.
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
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
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,
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.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
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.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
Design issues for Object-Oriented Languages
Dynamic Allocation in C
Object Lifetime and Pointers
Intro to Java L. Grewe.
Memory Management with Classes
Storage Management.
Chapter 8 Classes and Objects
Continuing Chapter 11 Inheritance and Polymorphism
CSE 303 Concepts and Tools for Software Development
Implementing Subprograms
Chapter 11: More on the Implications of Inheritance
Dynamic Memory Management
Java Programming Language
Chapter 11 Inheritance and Polymorphism Part 2
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:

C11, Implications of Inheritance

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 Shape x,y describe() Circle radius describe() Square side describe() … Shape form = new Circle(10,10,5); form.describe(); form = new Square(15,20,10);

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

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

Garbage collection (GC) Heap-based memory not automatically recovered on method exit (unlike stack) Manual management error-prone (free 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)