Docsity

Encapsulation and Subtyping - Software Foundations | CIS 500, Study notes of Computer Science

Partial preview of the text

Download Encapsulation and Subtyping - Software Foundations | CIS 500 and more Study notes Computer Science in PDF only on Docsity!

CIS 500

Software Foundations

Fall 2006

November 27

Recap

Multiple representations

All the objects in all the examples have type Counter (and sometimes more specific types).

But their internal representations vary widely.

Encapsulation

An object is a record of functions, which maintain common internal state via a shared reference to a record of mutable instance variables. This state is inaccessible outside of the object because there is no way to name it. (Lexical scoping ensures that instance variable records can only be named inside the methods.)

Inheritance

Classes are data structures that can be both extended and instantiated. We modeled inheritance by copying implementations of methods from superclasses to subclasses. Each class I (^) waits to be told a record r of instance variables and an object this (which should have the same interface and be based on the same record of instance variables) I (^) uses r and this to instantiate its superclass I (^) constructs a record of method implementations, copying some directly from super and implementing others in terms of this and super. The this parameter is “resolved” at object creation time using fix.

Where we are...

What’s missing (wrt. Java, say)

We haven’t really captured the peculiar status of classes (which are both run-time and compile-time things) — we’ve captured the run-time aspect, but not the way in which classes get used as types in Java.

Also not named types with declared subtyping Nor recursive types

Nor run-time type analysis (casting, etc.) (... nor lots of other stuff)

Modeling Java

Models of Java

Lots of different purposes −→ lots of different kinds of models I (^) Source-level vs. bytecode level I (^) Large (inclusive) vs. small (simple) models I (^) Models of type system vs. models of run-time features (not entirely separate issues) I (^) Models of specific features (exceptions, concurrency, reflection, class loading, ...) I (^) Models designed for extension

Featherweight Java

Purpose: model “core OO features” and their types and nothing else.

History: I (^) Originally proposed by a Penn PhD student (Atsushi Igarashi) as a tool for analyzing GJ (“Java plus generics”), which later became Java 1. I (^) Since used by many others for studying a wide variety of Java features and proposed extensions

Things left in

I (^) Classes and objects I (^) Methods and method invocation I (^) Fields and field access I (^) Inheritance (including open recursion through this) I (^) Casting

Example

class A extends Object { A() { super(); } }

class B extends Object { B() { super(); } }

class Pair extends Object { Object fst; Object snd;

Pair(Object fst, Object snd) { super(); this.fst=fst; this.snd=snd; }

Pair setfst(Object newfst) { return new Pair(newfst, this.snd); } }

Formalizing FJ

Nominal type systems

Big dichotomy in the world of programming languages: I (^) Structural type systems: I (^) What matters about a type (for typing, subtyping, etc.) is just its structure. I (^) Names are just convenient (but inessential) abbreviations. I (^) Nominal type systems: I (^) Types are always named. I (^) Typechecker mostly manipulates names, not structures. I (^) Subtyping is declared explicitly by programmer (and checked for consistency by compiler).