DOC PREVIEW
Penn CIS 500 - CIS 500 LECTURE NOTES

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CIS 500Software FoundationsFall 2006November 20On to ObjectsA Change of PaceWe’ve sp ent the sem es ter developing tools for defining andreasoning about a variety of programming language features.Now it’s time to use these tools for something more ambitious.Case study: object-oriented programmingPlan:1. Identify some characteristic “core features” of object-orientedprogramming2. Develop two different analyses of these features:2.1 A translation into a lower-level language2.2 A direct, high-level formalization of a simple object-orientedlanguage (“Featherweight Java”)The Translational AnalysisOur first goal will be to show how many of the basic features ofobject-oriented languagesdynamic dispatchencapsulation of stateinheritancelate binding (this)supe rcan be understood as “derived forms” in a lower-level languagewith a rich collection of primitive features:(higher-order) functionsrecordsreferencesrecursionsubtypingThe Translational AnalysisFor simple objects and classes, this translational analysis worksvery well.When we come to more complex features (in particular, classeswith this), it becomes less satisfactory, leading us to the moredirect treatment in the following chapter.ConceptsThe Essence of ObjectsWhat “is” object-oriented programming?A precise definition has been the subject of debate for decades.Such arguments are always inconclusive and seldom interesting.However, it is easy to identify some core features that are sharedby most OO languages and that, together, support a distinctiveand useful programming style.The Essence of ObjectsWhat “is” object-oriented programming?A precise definition has been the subject of debate for decades.Such arguments are always inconclusive and seldom interesting.However, it is easy to identify some core features that are sharedby most OO languages and that, together, support a distinctiveand useful programming style.The Essence of ObjectsWhat “is” object-oriented programming?A precise definition has been the subject of debate for decades.Such arguments are always inconclusive and seldom interesting.However, it is easy to identify some core features that are sharedby most OO languages and that, together, support a distinctiveand useful programming style.Dynamic dispatchPerhaps the most basic characteristic of object-orientedprogramming is dynamic dispatch: when an operation is invoked onan object, the ensuing behavior depends on the object itself, ratherthan being fixed (as when we apply a function to an argument).Two objects of the same type (i.e., responding to the same set ofope rations) may be implemented internally in completely differentways.Example (in Java)class A {int x = 0;int m() { x = x+1; return x; }int n() { x = x-1; return x; }}class B extends A {int m() { x = x+5; return x; }}class C extends A {int m() { x = x-10; return x; }}Note that (new B()).m() and (new C()).m() invoke completelydifferent code!EncapsulationIn most OO languages, each object consists of some internal stateencapsulated with a collection of method implementationsope rating on that state.Istate directly accessible to methodsIstate inacces sible from outside the objectEncapsulationIn Java, encapsulation of internal state is optional. For fullencapsulation, fields must be marked protected:class A {protected int x = 0;int m() { x = x+1; return x; }int n() { x = x-1; return x; }}class B extends A {int m() { x = x+5; return x; }}class C extends A {int m() { x = x-10; return x; }}The code (new B()).x is not allowed.Side note: Objects vs. ADTsThe encapsulation of state with methods offered by objects is aform of information hiding.A somew hat different form of information hiding is embodied inthe notion of an abstract data type (ADT).Side note: Objects vs. ADTsAn ADT comprises:IA hidden representation type XIA collection of operations for creating and manipulatingelements of type X.Similar to OO encapsulation in that only the operations providedby the ADT are allowed to directly manipulate elements of theabstract type.But different in that there is just one (hidden) representation typeand just one implementation of the operations — no dynamicdispatch.Both styles have advantages.Caveat: In the OO community, the term “abstract data type” isoften used as more or less a synonym for “object type.” This isunfortunate, since it confuses two rather different concepts.Subtyping and EncapsulationThe “type” (or “interface” in Smalltalk terminology) of an objectis just the set of operations that can be performed on it (and thetypes of their parameters and results); it does not include theinternal representation.Object interfaces fit naturally into a subtype relation.An interface listing more operations is “better” than onelisting fewer operations.This gives rise to a natural and useful form of polymorphism: wecan write one piece of code that operates uniformly on any objectwhose interface is “at least as good as I” (i.e., any object thatsupports at least the operations in I).Example// ... class A and subclasses B and C as above...class D {int p (A myA) { return myA.m(); }}...D d = new D();int z = d.p (new B());int w = d.p (new C());InheritanceObjects that share parts of their interfaces will typically (thoughnot always) share parts of their behaviors.To avoid duplication of code, want to write the implementations ofthese behaviors in just one place.=⇒ inheritanceInheritanceBasic mec hanism of inheritance: classesA class is a data structure that can beIinstantiated to create new objects (“instances ”)Irefined to create ne w classes (“subclasses”)N.b.: some OO languages offer an alternative mechanism, calleddelegation, which allows new objects to be derived by refining thebe havior of existing objects.Exampleclass A {protected int x = 0;int m() { x = x+1; return x; }int n() { x = x-1; return x; }}class B extends A {int o() { x = x*10; return x; }}An instance of B has methods m, n, and o. The first two areinherited from A.Late bindingMost OO languages offer an extension of the basic mechanism ofclasses and inheritance called late binding or open recursion.Late binding allows a method within a class to call another methodvia a special “pseudo-variable” this. If the second method isoverridden by some subclass, then the behavior of the first methodautomatically changes as well.Though quite useful in many situations, late binding is rathertricky, both to define (as we will see) and to use appropriately. Forthis


View Full Document

Penn CIS 500 - CIS 500 LECTURE NOTES

Download CIS 500 LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CIS 500 LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CIS 500 LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?