Object Model. Object Oriented Programming Spring 2015

Similar documents
Object Model. Object Oriented Programming Winter

Case Study: Meta Classes

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Polymorphism. Zimmer CSCI 330

CMSC 132: Object-Oriented Programming II

Concepts of Programming Languages

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Java Magistère BFA

CPS 506 Comparative Programming Languages. Programming Language

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CS 251 Intermediate Programming Inheritance

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

JavaScript: Sort of a Big Deal,

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Polymorphism. return a.doublevalue() + b.doublevalue();

Strict Inheritance. Object-Oriented Programming Spring 2008

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

OBJECT ORİENTATİON ENCAPSULATİON

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Exercise: Singleton 1

C++ Important Questions with Answers

Data Abstraction. Hwansoo Han

C++ Programming: Polymorphism

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Francesco Nidito. Programmazione Avanzata AA 2007/08

Francesco Nidito. Programmazione Avanzata AA 2007/08

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Programming Languages 2nd edition Tucker and Noonan"

CS558 Programming Languages Winter 2013 Lecture 8

Check out Polymorphism from SVN. Object & Polymorphism

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CMSC 330: Organization of Programming Languages

Java: introduction to object-oriented features

Inheritance, Polymorphism and the Object Memory Model

CS260 Intro to Java & Android 03.Java Language Basics

Java Fundamentals (II)

Example: Count of Points

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Object Model Comparisons

The role of semantic analysis in a compiler

VIRTUAL FUNCTIONS Chapter 10

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Squeak Object Model. Technion - Israel Institute of Technology. Updated: Spring Object-Oriented Programming 1

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

C++ Inheritance and Encapsulation

Object Oriented Design

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

INHERITANCE. Spring 2019

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Inheritance. Transitivity

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Selected Java Topics

Properties of an identifier (and the object it represents) may be set at

Instance Members and Static Members

Object Oriented Programming. Spring 2008

Compaq Interview Questions And Answers

Types and Type Inference

CSE 401/M501 Compilers

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Strict Inheritance. Object-Oriented Programming Winter

Introduction to Programming Using Java (98-388)

Object typing and subtypes

The major elements of the object-oriented model

Inheritance & Polymorphism. Object-Oriented Programming Spring 2015

Java Object Oriented Design. CSC207 Fall 2014

A simple reflective object kernel

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

Lecture 9 : Basics of Reflection in Java

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Super-Classes and sub-classes

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Chapter 6 Introduction to Defining Classes

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

Static and Dynamic Behavior לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Java Overview An introduction to the Java Programming Language

CLASS DESIGN. Objectives MODULE 4

Inheritance and Interfaces

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Dynamically-typed Languages. David Miller

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Some instance messages and methods

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

UMBC CMSC 331 Final Exam

Class, Variable, Constructor, Object, Method Questions

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

CS263: Runtime Systems Lecture: High-level language virtual machines

Polymorphism and Interfaces. CGS 3416 Spring 2018

Transcription:

Object Model Object Oriented Programming 236703 Spring 2015

Class Representation In Memory A class is an abstract entity, so why should it be represented in the runtime environment? Answer #1: Dynamic Binding The actual method bound to a message is determined at run time based on the type (class) of the receiver There must be a link between the receiver and its methods Since links are the same for all objects of the same class, it makes sense to share the representation Message Object Class representation Method Table Method 1 Method 2 Method n 2

Dynamic Binding C++ Style // class B declares virtual f(), g() & h() B* b1 = GetSomeDerived(); // return new D1 B* b2 = GetSomeDerived(); // return new D1 B* b3 = GetSomeDerived(); // return new D2 D1 #1 D1 #2 D2 #1 Must this be an object? D1::f() D2::f() Method Table 1 D1::g() Method Table 2 D2::g() D1::h() D2::h() 3

Class Representation In Memory A class is an abstract entity, so why should it be represented in the runtime environment? Answer #2: Run-time type information Being able to recognize an object s type is sometimes useful: C++ is a downcast safe? Java are we comparing objects of the same type? Squeak are those arguments of the expected type? Better rely on polymorphism when possible, though 4

Type Comparison Java Style public boolean equals(object obj) { if (obj instanceof MyClass) { // cast and compare } } obj Must this be an object?? obj class MyClass 5

Class Representation In Memory A class is an abstract entity, so why should it be represented in the runtime environment? Answer #3: Reflection The ability of the program to examine and possibly modify itself Serialization, cloning, code annotation, garbage collection etc. 6

Object Serialization Squeak Style storeon: astream... Must this be an object? 1 to: (self class instsize) do: [ :i astream nextputall: ' instvarat: '; store: i; nextputall: ' put: '; store: (self instvarat: i);... obj 7

A Class as an Object? There are various reasons for classes to have some memory representation Why not as an object? Answer: Let's go for it! Each object o has a pointer to its class object c, which is the object representing the class to which o belongs. Class Novel Play 1984 War & Peace Othello Oliver Twist Macbeth As you like 8

Meta-classes Meta: Beyond; transcending; more comprehensive Class: generates instances Metaclass: a class whose instances are classes Terminal Instance: cannot be instantiated further Objects Classes Instantiable objects Metaclasses Instances are classes Terminal Instances Non-Instantiable objects ( regular objects) 9

Taxonomy of Metaclass Systems 1-Level System: Objects only Objects describe themselves No classes: objects are instantiated or inherited from other objects A.K.A. prototype inheritance Example: Self, JavaScript 2-Level System: Objects, Classes Objects are described by classes Classes do not exist in run-time. Not first-class objects: Not instances of classes Not created by constructors Cannot receive messages Examples: Eiffel, C++ 10

Taxonomy of Metaclass Systems 3-Level System: Objects, Classes, One Metaclass Objects described by classes Classes described by the metaclass The metaclass describes itself Examples: Little Smalltalk, Java, C# 4/5-Level System: Objects, Classes, Metaclasses, Objects described by classes Classes described by matching metaclasses Metaclasses described by meta-metaclass/es Example: Squeak 11

The 1 Level System In a 1-level system, an object is a essentially a map from names (strings) to either values or methods Very flexible, but static type checking is hard No distinction between objects and classes Every object is the class of itself Instantiation of a new object: Clone an existing object Create an empty object ( ex nihilo ) Possibly initialize using an object literal: obj = {a: 1, b: 2} 12

The 1 Level System Object Prototype Print <method> Oliver Twist Prototype Title Oliver Twist Title: <method> Author Dickens Author: <method> Clone Book Prototype Prototype Borrow <method> Novel Prototype Prototype Title??? Title: <method> Author??? Author: <method> 1984 Prototype Title 1984 Title: <method> Author Orwell Author: <method> Clone 13

Test case: JavaScript Prototype inheritance: run time linking instead of compile time subclassing. Lookup relies on delegation. var obj1 = {}; obj1.m = 1; var obj2 = {f: function(){}}; obj2. proto = obj1; console.log(obj2.m); // print 1 obj2.m = 2; // empty object, inherits Object // create and set field m // object with method // obj2 now "inherits" obj1 // new field shadows obj1.m console.log(obj1.m); // still prints 1 14

1 Level System Pros and Cons Pros: Extremely flexible Delegation can replace inheritance, but not the other way around As Object Oriented as it gets (literally) Cons: Performance penalty on member lookup Associative arrays, linked sub-objects No compile time validations and optimizations Not very common 15

The 2 Level System Objects are instances of classes Classes are compile time creatures No run time representation Minimal run time overhead Zero overhead ("supermarket") principle But objects types cannot be determined On some cases, run-time type information is available No class (static) members More on that when we discuss the 3 level system 16

Test case: C++ class C { public: int i; void f(); }; C::f() No class representation just a function int main() { C* c = new C; c->i = 7; c->f(); } 7 The class type just a field Note: Polymorphic types have a vtable and RTTI, which can be considered a 2.5 level 17

The 3 Level System Objects are instances of classes Classes are also objects All classes are instances of one metaclass In some languages, the metaclass class is named Class Simple and elegant model: courtesy of Smalltalk-76, the first language to introduce the metaclass concept Later adopted by Little-Smalltalk 18

Class Variables and Class Methods An Object Foo() Bar() x y The Class New() Method dict. Var. list The Meta Subclass() Method dict. Var. list A user-defined class can not affect the structure of its class object It can, however, affect the class object s state Can it affect the meta-class structure or state? 19

The Metaclass Class (3 levels) The only metaclass in the system Instance of itself Avoids the infinite regress of the instantiation relationship Inherits from Object Holds behavior common to all class objects: How to add methods How to instantiate a class Holds structure common to all class objects: Super class Instance variables List of methods Uniform behavior and structure of all classes: Different classes cannot have different new methods The new defined in class Class calls the constructor defined in the object s class 21

The 3 Level System Object Class Instance of Instance of Oliver Twist Instance of Author: Dickens Subclass of Fields Methods Novel Instance of Subclass of? Print Subclass of Fields Methods Instance of Subclass of Fields Methods New Add subclass Year: 1838 Fields Methods Author Year Set author Get author Set year Get year 22

Class Members in 3 Level Systems Object members are defined in the object s class Similarly, class members are defined in the metaclass. But there is only one, hence shared among all classes! Static binding allows emulation Static members are stored in some global space The class is merely a namespace e.g., Math.abs(x) Java, C#, C++ etc. 23

The 4 Level System Each object is an instance of a class Each class is an instance of a metaclass May have its own singleton metaclass May share a metaclass with other classes Each metaclass is an instance of a meta-metaclass One meta-metaclass: Instance of itself Inherits from Object (or Class) Examples: (with minor variations) LOOPS: Lisp Object and data Oriented Programming System ObjVlisp: OBJect Virtual extension of LISP A model more than a real language 24

A 4 Level System Object Instance of Print New Oliver Twist Instance of Subclass of Fields Methods Novel Meta Object Instance of Subclass of Fields Methods New Add subclass Subclass of Fields Methods Instance of Instance of Meta Novel Class Author: Dickens Subclass of Instance of Instance of Year: 1838 Fields Subclass of Subclass of Methods Fields Fields Novel count: 1 Methods Methods Set author Get author Set year Get year Author Year Add novel Del novel Novel count 25

A 5 Level System Three Kinds of Entities 1. Objects as before 2. Classes as before 3. Metaclasses the object describing class X is an instance of a singleton metaclass X class Integer Integer class Class Class class Metaclass Metaclass Class Integer class Integer Metaclass class Metaclass Object class Object The metaclass Integer class is also the result of evaluating the Smalltalk expression Integer class 12 13 26

Inheritance in the 5 Level System Metaclasses hierarchy: X inherits from Y X class inherits from Y class A metaclass object X class : is an instance of Metaclass inherits from Class Class: an abstract class parent of all X class objects Breaks the model Object Integer Class Object class Integer class Class class Metaclass 27

Infinite Object-Class Regression 3-KRS: an infinite number of levels system Objects Classes Metaclasses Metametaclasses Meta-meta-metaclasses For each object X, there is a meta-object X Meta-object Class Class = object abstraction; meta-object = object reflection Lazy creation of meta-objects enables the system to work in a finite world In real world: Ruby s singleton class! X X X X X 28