7 targetType = this.parentType;8 }9 // virtualBinding is applicable only if the default binding fails10 constraint bottomUpResolution = skip(defaultBinding, virtualBinding);11 // initialize the instance variables and activate binding.12 method subclassOf(childType, parentType) {...}13 }Each instance of MethodInheritance defines an inheritance relationbetween the childType and parentType. The virtualBindingin this example selects <strong>all</strong> c<strong>all</strong>s to the childType and reroutesthese to the parentType, effectively specifying virtual methodlookup. This virtual lookup only takes place if regular lookupfails, specified by the constraint bottomUpResolution: If the defaultbinding succeeds, the virtualBinding is skipped.5. DEFINING INHERITANCE OF FIELDSDefining the availability and accessibility of fields from superclassesis needed for expressing various inheritance- (andother composition-) semantics. Our previous version of theCo-op language, Co-op/I, did not support this; this paperexplains the application of controlling field composition semanticsfor the first time.For example, the Sm<strong>all</strong>talk style inheritance we have definedearlier, also creates FieldInheritance. Even though thefields of a Person are not directly accessible from its subclassStudent, they are still addressable through methods definedin the class Person. Thus, upon creation of a child class, also<strong>all</strong> fields in its superclasses must be created. That is whatthe composition operator FieldInheritance does.FieldInheritance also creates a relation between an instanceof the child class, and an instance of its superclass 1 . ForSm<strong>all</strong>talk style inheritance, this relation is created using theLocalFieldAccess operator, which <strong>all</strong>ows private field access only.Now, consider you do not want field access to be limitedto the declaring class only, but you want them to be alsoaddressable through the child classes. For example, <strong>all</strong>owingus to write the following implementation of Student:1 class Student {2 method study() {3 return ”Is that necessary for ” + this.title + ” ” + this.name + ”?”;4 }5 }The field accesses in the method study() address fields definedin the super class, just like the way we can address methodsdefined in the super class. Therefore, defining a bindingwhich realizes accessing fields through child classes can bedone in a similar way to defining it for methods:1 class InheritedFieldAccess {2 var child;3 var parent;4 // Binding for field lookup in parent type5 binding inheritFields = (messageKind == ”Lookup”6 &target==this.child){7 target = this.parent;8 targetType = System.classOf(this.parent);9 this = this.parent;10 }11 // inheritFields is applicable only if the default binding fails12 constraint bottomUpResolution = skip(defaultBinding, inheritFields);13 // initialize instance and activate binding14 method initDispatch(child, parent) { ... }15 }The most notable difference with MethodInheritance is that inthis case we have a parent and child instance instead of type,1 This does not involve specific and optimized memory layoutsfor objectsbecause field values are instance properties, whereas methodbehavior is a property of the type.Now, we can use field inheritance as shown in the examplebelow. First, we create a relation between the parent andchild type and then we can access <strong>all</strong> fields of the parenttype also through any instance of a child type, which themethod study() does.1 MethodInheritance.subclassOf(Student, Person);2 FieldInheritance.subclassOf(Student, Person, InheritedFieldAccess);3 var alice = Student.new(”Alice”, ”Bachelor”);4 System.println( alice.study() );Languages like Java and C++, which enable inheritanceof fields, also provide mechanisms to let the developer selectfields which are not accessible through subtypes. We havemodeled this using the SelectiveFieldInheritance composition operator,which can replace the InheritedFieldInheritance. This operatoronly enables inheritance for fields that are selectedexplicitly. Its use is as follows:1 MethodInheritance.subclassOf(Student, Person);2 var selectiveInh = SelectiveFieldInheritance.new();3 selectiveInh.initDispatch(Student, Person);4 selectiveInh.addField(”name”);5 var alice = Student.new(”Alice”, ”Bachelor”);6 System.println( alice.study() );Since this example <strong>all</strong>ows subclasses of Person only accessto the field name, the implementation of Student shown in thebeginning of this section will yield a runtime error. However,the following implementation of Student is correct when thisselective inheritance is applied:1 class Student {2 method study() {3 return ”Is that necessary for ” + this.name + ”?”;4 }5 }Traditional programming languages <strong>all</strong>ow programmers toadd access rules to fields or methods by adding a modifierto their declaration. To enable a similar programming style,Co-op <strong>all</strong>ows the addition of annotations to field and methoddeclarations. Using reflective capabilities to access theseannotations—which are not yet possible in the Co-op/IIprototype—<strong>all</strong>ows implementing the FieldInheritance compositionoperator in such a way that it, for example, only appliesto fields with the @Inherited annotation:1 class Person {2 var @Inherited name;3 var title;4 }6. FREEING FIELD ACCESS BEHAVIOR FROMKEYWORDSIn most (OO) programming languages, there are manykeywords and fixed language constructs to manipulate theway that data is, or can be, accessed. We mention a fewexamples:• Access modifiers in Java, C++ and C# are public, protected,and private. A language like C++ adds a friendkeyword to express yet another form of access rightson data (as well as behavior). Note that there is a widerange of possible access modifiers, when including thenotion of package-level protection, or the distinctionbetween class-level vs instance-level protection.• The Java, C++ or C# keyword static controls whether<strong>all</strong> instances of a class share a field, or each has its owncopy.
• The keywords final in Java, const in C++ or readonlyin C# declare special semantics to the usage of thevariable (i.e., the variable may be assigned only once).For every new keyword or feature that is introduced in alanguage, it must be considered carefully how this interactswith <strong>all</strong> possible combinations of other language constructs.This can be very ch<strong>all</strong>enging, and also tends to make the evolutionof the language over time very difficult. As a result,creating new language constructs to address <strong>all</strong> the desiredfeatures in one language is not feasible.One of the possible work-arounds is the adoption of designpatterns [5], which document and standardize solutionsto common design problems. However, implementing designpatterns requires code changes and additions in multiple locations,with the concept and identity of the adopted designpattern being lost [14, 8].Our proposal, as illustrated in the previous sections, is toaim for a simple object model, and a single mechanism forexpressing a wide range of behavioral modifiers for fields.Examples of modified composition semantics, which can beexpressed with the proper composition operators in Co-op/II,are: access modifiers, static, synchronized, final, and soforth, but also more conceptual constructs such as automaticconversions, checking of validity constraints, persistence,transactions, or expressing roles. Composition operatorssupporting these semantics can be provided by reusablelibraries. In [1], larger complementary examples of behavioralcomposition are presented.7. RELATED WORKOstermann and Mezini argue in [8], fully in line with ourreasoning that “[..] often non-standard composition semanticsis needed, with a mixture of properties, which is notas such provided by any of the standard techniques.”. Toaddress this, they propose a sm<strong>all</strong> design space of propertiesof composition languages, of which Overriding of members,Transparent redirection of access to pseudovariables,and Acquisition, or transparent forwarding of access. Thisdesign space specific<strong>all</strong>y covers the range of compositionsfrom object aggregation to inheritance, but is unable toexpress other types of compositions, such as predicate dispatch,aspects, andsoforth. A key technique proposed intheir work are Compound References, which are exploited toexpress various alternative visibility and sharing styles fordata fields. Our approach differs among others in the abilityto express crosscutting abstractions, and the ability toinfluence field access (dynamic<strong>all</strong>y) based on predicates.Open classes [3], later c<strong>all</strong>ed inter-type declarations inAspectJ [15], <strong>all</strong>ow for flexibly extending classes with additionalfields, expressed separately from the original classdefinitions. This <strong>all</strong>ows for application-specific extension ofclasses with additional fields. Using advice on field read orwrite join points, it is also possible in aspect-oriented languagesor frameworks to implement conceptual modifiers forfield accesses. Examples are adding persistence 2 or definingaccess permissions in the style of multilevel security forfields [9]. There is no notion of generalizing such extensionsto an application-independent composition operator.On another note, aspect-oriented languages like AspectJdo not only <strong>all</strong>ow for influencing field access and adding2 See the SourceForge project “Java Persistence Aspect” athttp://sourceforge.net/projects/jpa/.fields to classes; they also introduce the new mechanism ofimplicit object instantiation which is controlled by so-c<strong>all</strong>edaspect instantiation policies. Such policies declare when newaspect instances are shared between different (implicit) invocationsand when a new instance has to be created. Compose*[4] extends this concept and supports instantiationpolicies per field in an aspect instance. In Co-op, we believe,<strong>all</strong> these mechanisms can be realized uniformly as compositionoperators.In [2], Bracha and Lindstrom discuss that the class construct(in languages with inheritance) has many differentroles (nine, when ignoring type-related issues). They proposeto adopt a very simple model of classes, which canthen be enhanced by applying operators over modules, toexpress a wide range of inheritance semantics. As such ithas similar aims as Co-op, but focuses only on inheritancelikesemantics (e.g. it is not able to express aspects), and itdiffers in its mechanism which is purely static; JIGSAW is amodule manipulation language, where our composition operatorsexpress various composition semantics through firstclassmodules (expressed in the same language).Reflex [12] is a reflection-based kernel for AOP languages.It also supports structural aspects, which may involve addition(and possibly modification) of members. In [13], structuralaspects are discussed, focussing on the detection of interactionsbetween multiple, additive, and structural aspectexpressions.Reflective languages and systems in general offer low-levelconstructs for influencing, among others, the message dispatchprocess. However, for application programmers, theylack the abstractions provided by Co-op such as bindingsand constraints that enable the structured and composableexpression of reusable composition operators. Nevertheless,fully reflective languages can be a suitable means for implementinglanguages like Co-op on top of them, or for implementinga meta-object protocol which offers comparableabstractions.8. CONCLUSIONS AND FUTURE WORKThe Co-op approach enables developers to freely defineand use operators realizing composition mechanisms in theirprograms. This is facilitated through the simple object modeland the concept of declarative bindings defined in normalclasses which can rewrite message sends. Because of thisfirst-class nature of composition operators, they can be reusedand composed again to enable a decomposition of softwaremost natural to its problem domain.The contribution of this paper is the representation offield accesses as message sends in Co-op/II and thus exposingfield accesses to composition operators. To demonstratethe appropriateness of this approach we have expressed awide range of composition mechanisms for fields, like differentaccess policies for shared data in class hierarchies orbehavioral modifiers for fields 3 . An example which is notdescribed in this paper is static field access.There are several ch<strong>all</strong>enges for such a composition operatortechnique; firstly, expressiveness: the technique shouldbe able to express the desired semantics. This puts requirementson the language for expressing the composition behavior,as well as on the access to the internals of the program(say, its representation and execution by an underlying vir-3 See the SourceForge project “Co-op” at co-op.sf.net