Concepts+Techniques+and+Models+of+Computer+Programming_Part19

Concepts+Techniques+and+Models+of+Computer+Programming_Part19

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 498 Object-Oriented Programming class Counter attr val meth init(Value) val:=Value end meth browse {Browse @val} end meth inc(Value) val:=@val+Value end end Figure 7.1: An example class Counter (with class syntax) setting, when multiple threads use the objects. In particular, object locking is explained there. 7.2 Classes as complete ADTs The heart of the object concept is controlled access to encapsulated data. The behavior of an object is specified by a class . In the most general case, a class is an incremental definition of an ADT, that defines the ADT as a modification of other ADTs. There is a rich set of concepts for defining classes. We classify these concepts into two sets, according as they permit the class to define an ADT completely or incrementally: • Complete ADT definition . These are all the concepts that permit a class, taken by itself, to define an ADT. There are two sets of concepts: – Defining the various elements that make up a class (Section 7.2.3), namely methods, attributes, and properties. Attributes can be initial- ized in several ways, per object or per class (Section 7.2.4). – Taking advantage of dynamic typing. This gives first-class messages (Section 7.2.5) and first-class attributes (Section 7.2.6). This allows powerful forms of polymorphism that are difficult or impossible to do in statically-typed languages. This increased freedom comes with an increased responsibility of the programmer to use it correctly. • Incremental ADT definition . These are all the concepts related to in- heritance, that is, they define how a class is related to existing classes. They are given in Section 7.3. Copyright c 2001-3 by P. Van Roy and S. Haridi. All rights reserved. 7.2 Classes as complete ADTs 499 local proc {Init M S} init(Value)=M in (S.val):=Value end proc {Browse2 M S} {Browse @(S.val)} end proc {Inc M S} inc(Value)=M in (S.val):=@(S.val)+Value end in Counter=c(attrs:[val] methods:m(init:Init browse:Browse2 inc:Inc)) end Figure 7.2: Defining the Counter class (without syntactic support) 7.2.1 An example To see how classes and objects work in the object system, let us define an example class and use it to create an object. We assume that the language has a new construct, the class declaration. We assume that classes are first-class values in the language. This lets us use a class declaration as either statement or expression, in similar manner to a proc declaration. Later on in the chapter, we will see how to define classes in the kernel language of the stateful model. This would let us define class as a linguistic abstraction. Figure 7.1 defines a class referred to by the variable Counter . This class has one attribute , val , that holds a counter’s current value, and three methods , init , browse , and inc , for initializing, displaying, and incrementing the counter. The attribute is assigned with the := operator and accessed with the @ operator. This seems quite similar to how other languages would do it, modulo a different syntax.seems quite similar to how other languages would do it, modulo a different syntax....
View Full Document

This document was uploaded on 08/10/2011.

Page1 / 30

Concepts+Techniques+and+Models+of+Computer+Programming_Part19

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online