420 likes | 1.15k Views
Object-Oriented Programming Languages. Principles of Object-Oriented Software Development (Chapter 5). Objective. To illustrate the concepts of object-oriented programming languages in a broader context beyond the familiar Java and C++ interpretations. Outline. The object paradigm
E N D
Object-Oriented Programming Languages Principles of Object-Oriented Software Development (Chapter 5)
Objective • To illustrate the concepts of object-oriented programming languages in a broader context beyond the familiar Java and C++ interpretations.
Outline • The object paradigm • Comparison: Smalltalk, Eiffel, C++, Java • Dimensions of language design • Classless languages
The Notion of Object • The notion of objects appears in many areas of computer science • Software engineering: Abstract data types • Artificial intelligence: Frames • Databases: Semantic data models • Distributed systems: Capability-based computing
Perspectives on Object-Orientation • Structural • Capability of representing arbitrarily structures complex objects • An object is a data structure in memory • Operational • The ability to operate on complex objects through generic operators • An object represents an element of a conceptual model • Behavioral • The specification of types and operations • An object is a type, used for data abstraction
Characteristics of Object-Oriented Languages • Object creation facility • Must be able to instantiate objects • Message-passing capability • Must be able to communicate between objects • Class capability • Must have a mechanism for defining classes • Inheritance features • Must support some form of inheritance
Classifications of Object-Oriented Languages • Hybrid – extensions to existing languages • O-O retrofitted to existing languages: C, Lisp, Pascal, Prolog, etc • Examples: C++, CLOS, Objective Pascal, DLP • Frame-based – knowledge-based reasoning • Frame – a structure consisting of slots • Slot – a value of an attribute or a relation to other frames • Examples: KRL, LOOPS
Classifications (continued) • Distributed, concurrent, actor – parallel computing • Active object – executes in parallel with other active objects • Examples: Concurrent Smalltalk, sC++, POOL-T • Actor languages • Instead of threads, parallel execution is realized by self-replacement • Actor processes a message sent to it, and creates a successor object in its place, thus the same actor potentially takes on a different identity • Alternative object models • Removes the distinction between classes and objects • Everything is an object • Examples: Self
Object-Oriented Scripting Languages • Many scripting languages are designed to support object-oriented programming • Built-in support • Embedding an O-O language • Javascript • Perl, JPL (Java-Perl module) • Tcl/Tk, Jacl (Tcl/Java) • Python, JPython
Objects in Javascript <script language=Javascript> function object_display(msg) { object method return msg + ' (' + this.variable++ + ')'; } function myobject() { object constructor this.variable=0; this.display = object_display; return this; } var a = new myobject(); create object document.write(a.display("a message")); document.write(a.display("another message")); </script>
Outline • The object paradigm • Comparison: Smalltalk, Eiffel, C++, Java • Dimensions of language design • Classless languages
Comparing Smalltalk, Eiffel, C++ and Java • Criteria • Class libraries • Availability of sufficient class library support • Programming environment • Availability of environment to support development • Language characteristics
Smalltalk Example Behavior subclass: #Ctr instanceVariableNames: 'value' Ctr methodsFor: 'initialization' initialize value := 0. Ctr methodsFor: 'modifications' add: aValue value := value + aValue. Ctr methodsFor: 'inspection' value ^value
Eiffel Example class counter export inc val feature count : Integer create is do count := 0 end inc( n : Integer ) is require n > 0 do count := count + n ensure count = old count + n end val : Integer is do Result := count end invariant count >= 0 end -- class counter
C++ Example class ctr { public: ctr() { n = 0; } // constructor ~ctr() { cout << "bye"; }; // destructor void add( int i = 1) { n = n + i; } int val( ) { return n; } private: int n; }; // Usage: ctr c; c.add(1); cout << c.val(); ctr* p = new ctr(); c->add(1); cout << c->val();
Class Libraries • Availability of sufficient class library support • Smalltalk: part of language definition • Eiffel: part of language definition • C++: STL, large number of 3rd party libraries • Java: large number of standardized APIs
Programming Environment • What constitutes “good” programming environment? • Graphical interface for novices • Command line interface for experts • Smalltalk: part of language definition • Eiffel: part of language definition • C++: large number of commercial environments • Java: many popular IDEs (Eclipse, JBuilder, Visual J#, JDeveloper, NetBeans)
Language Characteristics • Uniformity of data structures • Documentation value • Reliability • Inheritance mechanism • Efficiency • Memory management • Language complexity
Uniformity • Uniformity of treatment of data types • Smalltalk • Every data type is a class • Eiffel • Elementary data types are distinct from classes • C++ • Elementary data types are distinct from classes • Java • Elementary data types are distinct from classes
Documentation Value • How easy is it to read a program and to write a correct program? • Smalltalk • A consistent style in writing programs because everything is a class • Eiffel • Special keywords to formally specify correctness of programs and specify interfaces • C++ • No constructs to support documentation • Terse style is preferred by some over the more verbose languages • Java • Javadoc sets a standard for documentation
Reliability • Smalltalk • Dynamically typed, no type checking • Eiffel • Static type checking, correctness assertions • C++ • Inherits unreliability perception from C • Static type checking within a compilation module, weak support across modules • Consistent type system • Java • Less error-prone than C++ due to absence of pointers and built-in garbage collection
Inheritance • Smalltalk • Single inheritance • Eiffel • Multiple inheritance • C++ • Multiple inheritance • Java • Single inheritance • Multiple interface inheritance
Efficiency • Smalltalk • Interpreted • Eiffel • Compiled • Dynamic binding for all methods • C++ • Compiled • Inline functions, flexible memory management, friends • No garbage collection • Java • Compiled to bytecode; efficiency depends on JVM
Outline • The object paradigm • Comparison: Smalltalk, Eiffel, C++, Java • Dimensions of language design • Classless languages
Design of Object-Oriented Languages • Object-oriented languages support the following: • Object – state + operations • Class – template for object creation • Inheritance – sharing parts of a description • Data abstraction – state accessible by operations • Strong typing – compile time checking Object-oriented = objects + classes + inheritance Object-based = objects + classes
Orthogonal Dimensions • More formally, object-oriented languages can be defined by the following orthogonal dimensions: • Objects – modular computing agents • Supports construction of modular units to which a principle of locality applies • Types – expression classification • Types are a more general abstraction than classes • Dynamic typing – no static type checking, only inability to evaluate an expression leads to runtime error • Static typing – compile time determination and checking of the types of all variables, objects and expressions • Delegation – resource sharing • A mechanism that allows redirection of control dynamically • A more general mechanism than forwarding a method call • Examples: single inheritance, multiple inheritance, scope inheritance • Abstraction – interface specification • What is visible and what is hidden to other objects? • External behavior specified by contracts • External state specified by public or protected attributes
Open Systems • A goal of object-oriented language design is openness • A software system is said to be open if its behavior can be easily modified and extended • Reactiveness • A program has a (runtime) choice between potential actions • Modularity • A program can be safely extended (at design time) by adding new components
Reactiveness • A program has a (runtime) choice between potential actions • Late binding (polymorphism) provides dynamic selection of alternatives depending on the subtype • Guards in concurrent languages provide a choice for accepting or rejecting a call
Modularity • A program can be safely extended (at design time) by adding new components • Languages must provide appropriate information hiding mechanisms: • Hide details of objects from outside world • Usual notion of encapsulation • Classes provide modularity by hiding details of the class from the outside world • Hide details of outside world from objects • Objects should not have to know that other objects are on the same machine or not • Objects should not have to know that other objects are active
Object-based Concurrency • Object-based concurrency = objects + processes • Approaches • Add processes as a primitive data type • Programmer has the burden of dealing with synchronization • Implement active objects • Objects are simultaneously active • Language provides constructs to support synchronous communications (rendezvous) • Active object interrupts itself to respond to messages • Potential for deadlock exists with self-invocation • Performance issues if only a few active objects needed • Need careful choice in determining which objects should be active • Use asynchronous communication through message buffers • Instead of interrupting, queue up the messages in a buffer • Real-time constraints become impossible to guarantee
Outline • The object paradigm • Comparison: Smalltalk, Eiffel, C++, Java • Dimensions of language design • Classless languages
Classless Languages • Classical object model • Objects are instances of classes Object-oriented = objects + classes + inheritance • Classless languages • There are no classes, only objects • New objects are created by cloning existing objects • Objects are cloned from objects called “prototypes” • Inheritance is approximated by delegation • Object chooses which object to designate as parent • Dynamic binding is implemented by searching up the parent list • Advantages • Conceptually easier to create objects from existing examples rather than defining a characterization of the object through a class • Dynamic sharing of code and information is more flexible than inheritance and instantiation
Design Issues for Prototypes • State • Object consists of slots • Object consists of variables and methods • Creation • Shallow cloning – copy the object • Deep cloning – copy the object and all referenced objects • Delegation • Implicit delegation – follow the parent chain • Explicit delegation – object is named • Self language: slots, shallow cloning, implicit delegation
Improving Performance • In pure object-oriented languages, dynamic type checking and dynamic binding is the rule • Increases flexibility • Performance suffers • Solutions • Special-purpose hardware • Hybrid languages • Create a hybrid language with a procedural language • Dealing with unwanted interactions can be a complex problem • Static typing • Bounds the language flexibility • Dynamic compilation • Perform partial evaluation, lazy compilation, message splitting
Summary • The object paradigm • Notion of object – viewpoints • Classification – object extensions • Comparing Smalltalk, Eiffel, C++ and Java • Tradeoffs between pure and hybrid languages • Flexibility versus performance • Design dimensions of object-oriented languages • Object-oriented = objects + classes + inheritance • Orthogonal dimensions: objects, types, delegation, abstraction • Open systems are systems that can be easily modified and extended • Classless languages use cloning and delegation instead of instantiation and inheritance