Comparison of OO Programming Languages

Download Report

Transcript Comparison of OO Programming Languages

Comparison of OO
Programming Languages
© Jason Voegele, 2003
Characteristics of an OO Programming
Language
•
•
•
•
•
Encapsulation / Information Hiding
Inheritance
Polymorphism / Dynamic Binding
All pre-defined types are objects
All operations performed by sending messages to
objects
• All user-defined types are objects
C-S 546
2
OO Languages - Comparison
Properties
Eiffel
Smallta
lk
Ruby
Java
C#
C++
Visual
Basic
1
Yes
Yes
Yes
Yes
Yes
Yes
Yes?
2
Yes
Yes
Yes
Yes
Yes
Yes
No
3
Yes
Yes
Yes
Yes
Yes
Yes
Yes?
4
Yes
Yes
Yes
No
No
No
No
5
Yes
Yes
Yes
No
No
No
No
6
Yes
Yes
Yes
Yes
Yes
No
No
C-S 546
3
Observations
• Eiffel, Smalltalk and Ruby are the pure OO
languages
• Java and C# fail to meet properties 4 and 5; many
OO designers still accept that it is not a serious
issue.
• C++ does not satisfy properties 4, 5 and 6. Lack of
property 6 leads to inconsistencies in the set of
objects created in the program.
• VB is simply a procedural language with some
OO features – should not be considered to
implement an OO design
C-S 546
4
Static Vs Dynamic Binding
• Static binding
– Every variable has a fixed, permanent type attached to
the variable at compile time
• Dynamic binding
– A variable has no fixed type; its type changes
dynamically as the program executes
• Hybrid binding
– A variable has a fixed type assigned at compile time,
but it may assume other types during run-time through
polymorphic substitutions
C-S 546
5
Static and Dynamic Binding (continued)
• Smalltalk and Ruby are the only languages that
support dynamic binding
• Eiffel, Java, C# and C++ support hybrid binding
• Visual Basic supports only static binding
• When a language uses generic classes (templates),
it supports hybrid binding
– Eiffel, C# and C++
• Future version of Java is expected to support
generic classes
C-S 546
6
Inheritance
• Almost all OO languages, except VB, support
inheritance
• Multiple inheritance is supported by Eiffel and
C++
– Jason Voegele argues that Eiffel’s mechanisms to
handle conflicts in multiple inheritance is neat and
clear than those in C++
• Java does not support multiple inheritance
directly, but can be realized through interfaces
C-S 546
7
Class based inheritance Vs Object based
inheritance
• Most OO languages support class base inheritance
– A class designated as the subclass of a superclass
– Objects of subclasses can be substituted for objects of
superclass (polymorphism)
– Subtype inheritance
• Some languages support object based inheritance
– Objects can inherit features from other objects during run-time
• SELF and Javascript support this feature
• Ruby supports both class based and object based
inheritance
C-S 546
8
Method overloading and operator
overloading
• Method overloading
– Same name for more than one method
– Differentiated by the number and/or type of
parameters
– Supported by Java and C++ only
• Operator overloading
– Overloading the meaning of one or more of the
language symbols such as “+” and “*”
– Supported by Eiffel, Ruby, C++
C-S 546
9
Operator overloading - expectations
• All operations in the system must be messages to
objects
– Executions with operators are considered to be
equivalent to message calls
• Operators must have equivalent functional forms
–1+2
is the same as
1. + (2)
• Smalltalk, Ruby and Eiffel support both of these
features
• C++ supports neither, even though it supports
operator overloading
C-S 546
10
Higher order functions
• Functions whose arguments are functions
Another function
themselves
– Example: calculate_tax (find year(), tax_rate)
• The scope of the functions that are passed as
parameters must also be visible or computable
• Smalltalk and Ruby support higher order
functions; recent version of Eiffel supports too.
• Java, C# and C++ do not support this feature;
neither does VB.
C-S 546
11
Garbage Collection
• Mechanism to free memory of unused objects
• Mark and sweep method
– One phase marks the unused objects and another phase cleans
them up
– Both phases are done at unspecified time (programmer does not
know)
– Smalltalk, Eiffel, Ruby and Java support this mechanism
• Reference count mechanism
– Used by Visual Basic; has its own problems
• C++ does not support garbage collection
C-S 546
12
Class variables
• Variables that are declared as static in a class and are
accessible to all instances of that class
– Only one copy of each class variable exists and is shared by all
instances
• Smalltalk, Ruby, Java, C# and C++ support this feature
though the implementation mechanisms are slightly
different
• Smalltalk also supports additional classifications such as
“pool variables” that are shared by only a group of
classes
– An extended mechanism of class variables
C-S 546
13
Access Control
• Access to various features of a class are generally
declared as “public” or “private” in most OO languages
• Java, C# and C++ support “protected” by which private
features of a class can be accessed by its subclasses
– In these languages, a private feature can be accessed by any
member of the same object or objects of the same class as well
– C++ supports “friend” mechanism by which another class B can
access the private features of a class A as long as B is declared
as a friend of A  this violates the encapsulation property
• Eiffel supports selective export, much similar to
“protected”
• In Smalltalk, methods can be “public” or “private” but
attributes are strictly “private” only.
C-S 546
14
Design by Contract
• A notion introduced by Bertrand Meyer
• Each method is designed with a contract described
by “pre” and “post-conditions”, and “invariants”
• Eiffel is the only language that supports this
feature
• Java and Ruby has libraries that tend to support
the design by contract feature, but it is not at the
same level as in Eiffel
C-S 546
15
Concurrency
• Concurrency is generally supported in OO
languages by threads of execution control
• Almost all languages support multi-threading
• It is explicit in Java, C# and C++ but somewhat
indirect(?) in Smalltalk
• VB supports it through timers
• Ruby supports threading not only in its syntax but
a separate built-in interpreter, and hence does not
rely on OS support for its implementation.
C-S 546
16
Pointer Arithmetic
• Allows a program to directly manipulate memory
addressed through “pointers”.
– Goal is to improve run-time efficiency
• C++ is the only language that supports pointers
• Other OO languages tend not to support pointer
arithmetic because
– Pointers are not OO concepts/principles
– Misuse of (or improper use of) pointers will lead to a
lot of programming problems that are not part of the
application domain
C-S 546
17