Comparison of programming languages (object-oriented programming)

del.icio.us del.icio.us
Digg Digg
Furl Furl
Reddit Reddit
Rojo Rojo
Add to OnlyWire
This article is part of the
Programming Language Comparison
series.
General Comparison
Basic Syntax
Basic Instructions
Arrays
Associative arrays
String Operations
String Functions
List comprehension
Object-oriented programming
Object-oriented constructors
Database access

Evaluation strategy
List of "hello world" programs

Comparison of ALGOL 68 and C++
Compatibility of C and C++
Comparison of Pascal and Borland Delphi
Comparison of Pascal and C
Comparison of Java and C++
Comparison of Java and C#
Comparison of C# and Visual Basic .NET
Comparison of ABAP and Java

Contents


Object creation and destruction

creation destruction
C++ (STL) class variable«(parameters)»; or
class *variable = new class«(parameters)»;
delete pointer;
C# class variable = new class(parameters); variable.Dispose();[1]
Java variable.dispose();[1]
JavaScript var variable = new class (parameters) delete (variable);[1]
Python variable = class(parameters) del variable [1](Normally not needed)
Visual Basic .NET Dim variable As New class(parameters) variable.Dispose()[1]
PHP $variable = new class(parameters); unset($variable);
Perl «my »$variable = class->new(parameters); undef($variable);
Ruby variable = class.new«(parameters)» [1]
Windows PowerShell $variable = New-Object «-TypeName» class ««-ArgumentList» parameters» Remove-Variable «-Name» variable
OCaml let variable = new class «parameters» or
let variable = object members end[2]
[1]

File declaration

class interface namespace
C++ (STL) class name« : public parentclasses[3]» { members }; namespace name { members }
C# class name« : parentclass«, interfaces»» { members } interface name { members }
Java class name« extends parentclass»« implements interfaces» { members } package name; members
JavaScript [4] [5]
Python class name(parentclasses[3]):
Tab
members
same, declare methods with pass as body __all__ = [ member1,member2,... ]
Visual Basic .NET Class name« Inherits parentclass»« Implements interfaces»
members
End Class
Interface name
members
End Interface
Namespace name
members
End Namespace
PHP class name« extends parentclass»« implements interfaces» { members } interface name { members } namespace name; members
Perl package name; «@ISA = qw(parentclasses[3]);» members 1; package name; members
Ruby class name« < parentclass»
members
end
Windows PowerShell [5]
OCaml class name «parameters» = object «(self)» «inherit parentclass «parameters» «inherit parentclass «parameters» ...[3]»» members end

Class members

Constructors and destructors

constructor destructor/finalizer
C++ (STL) class(«parameters») «: initializers[6]» { instructions } ~class() { instructions }
C# class(«parameters») { instructions } ~class() { instructions }[7]
Java void finalize() { instructions }[7]
JavaScript function class(«parameters») { instructions } [5]
Python def __init__(self«, parameters»):
Tab instructions
def __del__(self):
Tab instructions
Visual Basic .NET Sub New(«parameters»)
instructions
End Sub
Overrides Sub Finalize()
instructions
End Sub
PHP function __construct(«parameters») { instructions } function __destruct() { instructions }
Perl sub new { my ($class«, parameters») = @_; my $self = {}; instructions ... bless($self, $class); return $self; } sub DESTROY { my ($self) = @_; instructions }
Ruby def initialize«(parameters)»
instructions
end
[5]
Windows PowerShell [5]
OCaml initializer instructions[8] [5]

Fields

public private protected friend
C++ (STL) public: type field; private: type field; protected: type field; [9]
C# public type field; private type field; protected type field; internal type field;
Java type field;
JavaScript this.field« = value»; var field« = value»; [5]
Python self.field = expression
Just assign a value to it in a method
[5]
Visual Basic .NET Public field As type Private field As type Protected field As type Friend field As type
PHP public $field; private $field; protected $field;
Perl $self->{field} = expression;
Just assign a value to it in a method
[5]
Ruby [5] @field = value
Just assign a value to it in a method
Windows PowerShell Add-Member
«-MemberType »NoteProperty
«-Name »Bar «-Value »value
-InputObject variable
[5]
OCaml [5] val «mutable» field = value [5]

Methods

basic/void method value-returning method
C++ (STL) void foo(«parameters») { instructions } type foo(«parameters») { instructions ... return value; }
C#
Java
JavaScript function foo(«parameters») { instructions }; or
var foo = function («parameters») { instructions }; or
var foo = new Function («"parameters1", "parameters2", ..., »"instructions");
function foo(«parameters») { instructions ... return value; };
Python def foo(self«, parameters»):
Tab
instructions
def foo(self«, parameters»):
Tab
instructions
Tab return
value
Visual Basic .NET Sub Foo(«parameters»)
instructions
End Sub
Function Foo(«parameters») As type
instructions
...
Return value
End Function
PHP function foo(«parameters») { instructions } function foo(«parameters») { instructions ... return value; }
Perl sub foo { my ($self«, parameters») = @_; instructions } sub foo { my ($self«, parameters») = @_; instructions ... return value; }
Ruby def foo«(parameters)»
instructions
end
def foo«(parameters)»
instructions
expression resulting in return value
end
or
def foo«(parameters
instructions
return value
end
Windows PowerShell Add-Member «-MemberType» ScriptMethod «-Name» foo «-Value» { «param(parameters)» instructions } -InputObject variable Add-Member «-MemberType» ScriptMethod «-Name» foo «-Value» { «param(parameters)» instructions ... return value } -InputObject variable
OCaml [5] method foo «parameters» = expression

Properties

How to declare a property named "Bar"

read-write read-only write-only
C++ (STL) [5]
C# type Bar {
get {
instructions ... return value; }
set {
instructions } }
type Bar { get { instructions ... return value; } } type Bar { set { instructions } }
Java [5]
JavaScript
Python def setBar(self, value):
Tab
instructions
def
getBar(self):
Tab
instructions
Tab return value
bar = property(getBar, setBar)
def getBar(self):
Tab
instructions
Tab return value
bar = property(getBar)
def setBar(self, value):
Tab
instructions
bar = property(fset = setBar)
Visual Basic .NET Property Bar As type
Get
instructions
Return value
End Get
Set (ByVal
Value As type)
instructions
End Set
End Property
ReadOnly Property Bar As type
Get
instructions
Return value
End Get
End Property
WriteOnly Property Bar As type
Set (ByVal Value As type)
instructions
End Set
End Property
PHP function __get($property) {
switch (
$property) {
case '
Bar' : instructions ... return value;
} }
function __set(
$property, $value) {
switch (
$property) {
case '
Bar' : instructions
} }
function __get($property) {
switch ($
property) {
case '
Bar' : instructions ... return value;
} }
function __set($property, $value) {
switch (
$property) {
case '
Bar' : instructions
} }
Perl [5]
Ruby def bar
instructions
expression resulting in return value
end
def bar=(value)
instructions
end
def bar
instructions
expression resulting in return value
end
def bar=(value)
instructions
end
Windows PowerShell Add-Member
«-MemberType »ScriptProperty
«-Name »Bar «-Value »{ instructions ... return value }
«-SecondValue »{ instructions }
-InputObject variable
Add-Member
«-MemberType »ScriptProperty
«-Name »Bar «-Value »{ instructions ... return value}
-InputObject variable
Add-Member
«-MemberType »ScriptProperty
«-Name »Bar -SecondValue { instructions }
-InputObject variable
OCaml [5]

Overloaded operators

unary binary index type cast
C++ (STL) type operatorsymbol() { instructions } type operatorsymbol(type operand2) { instructions } type operator[](type index) { instructions } operator returntype() { instructions }
C# static type operator symbol(type operand) { instructions } static type operator symbol(type operand1, type operand2) { instructions } type this[type index] {
get{
instructions }
set{
instructions } }
static explicit operator returntype(type operand) { instructions }
Java [5]
JavaScript
Python def __opname__(self):
Tab
instructions
Tab return
value
def __opname__(self, operand2):
Tab
instructions
Tab return
value
def __getitem__(self, index):
Tab instructions
Tab return value
def __setitem__(self, index, value):
Tab instructions
[5]
Visual Basic .NET Shared Operator symbol(operand As type) As type
instructions
End Operator
Shared Operator symbol(operand1 As type, operand2 As type) As type
instructions
End Operator
Default Property Item(Index As type) As type
Get
instructions
End Get
Set(ByVal
Value As type)
instructions
End Set
End Propery
Shared casttype[10] Operator CType(operand As type) As returntype
instructions
End Operator
PHP [5]
Perl use overload "symbol" => sub { my ($self) = @_; instructions }; use overload "symbol" => sub { my ($self, $operand2, $operands_reversed) = @_; instructions }; [5]
Ruby def symbol
instructions
expression resulting in return value
end
def symbol(operand2)
instructions
expression resulting in return value
end
def [](index)
instructions
expression resulting in return value
end
def []=(index, value)
instructions
end[citation needed]
[5]
Windows PowerShell [5]
OCaml

Member access

How to access members of an object x

method property/field
C++ (STL) x.method(parameters) or
ptr->method(parameters)
x.property or
ptr->property
C# x.method(parameters) x.property
Python
Java
Visual Basic .NET
Windows PowerShell
Ruby x.method«(parameters)»
PHP x->method(parameters) x->property
Perl x->method«(parameters)» x->{property}
JavaScript x.method(parameters) or
x["method"](parameters)
x.property or
x["property"]
OCaml x#method «parameters» [5]

Special variables

current object current object's parent object
C++ (STL) *this [11]
C# this base[12]
Java super[12]
JavaScript
Python self[13] super(current_class_name, self)[3]
Visual Basic .NET Me MyBase
PHP $this parent[12]
Perl $self[13] $self->SUPER[12]
Ruby self super«(args)»[14]
Windows PowerShell
OCaml self[15] super[16]

Type manipulation

Get object type Is instance of (includes subtypes) Upcasting Downcasting
C++ (STL) typeid(x).name() dynamic_cast<type *>(&x) != NULL [17] dynamic_cast<type*>(ptr)
C# x.GetType() x is type (type) x
Java x.getClass() x instanceof type
JavaScript typeof x [18]
Visual Basic .NET x.GetType() TypeOf x Is type [17] CType(x, type)
Python type(x) isinstance(x, type) [18]
PHP get_class(x) x instanceof class
Perl ref(x) x->isa("type")
Ruby x.class or
x.type
x.instance_of?(type) or
x.kind_of?(type)
Windows PowerShell x.GetType() x -is [type] [17] [type]x
OCaml [19] x :> type [5]

See also

Notes

  1. ^ a b c d e f g This language uses garbage collection to release unused memory.
  2. ^ OCaml objects can be created directly without going through a class.
  3. ^ a b c d e This language supports multiple inheritance. A class can have more than one parent class
  4. ^ In JavaScript, all class members are declared in its constructor
  5. ^ a b c d e f g h i j k l m n o p q r s t u v w x y z This language does not support this feature
  6. ^ An optional comma-separated list of initializers for member objects and parent classes goes here. The syntax for initializing member objects is "member_name(parameters)" This works even for primitive members, in which case one parameter is specified and that value is copied into the member. The syntax for initializing parent classes is "class_name(parameters)". If an initializer is not specified for a member or parent class, then the default constructor is used.
  7. ^ a b This is a finalizer rather than a destructor. It is called by the garbage collector when an object is about to be garbage-collected. There is no guarantee on when it will be called or if it will be called at all.
  8. ^ This "initializer" construct is rarely used. Fields in OCaml are usually initialized directly in their declaration. Only when additional imperative operations are needed is "initializer" used. The "parameters to the constructor" in other languages are instead specified as the parameters to the class in OCaml. See the class declaration syntax for more details.
  9. ^ In C++, you don't declare specific fields to be accessible by outside things. Rather, you declare outside functions and classes to be friends to have access to the class's fields. See friend function and friend class for more details.
  10. ^ Where casttype is either Widening(may not cause data loss) or Narrowing(data loss possible).
  11. ^ Microsoft Visual C++ provides a non-standard keyword "__super" for this purpose; but this is not supported in other compilers because multiple inheritance is possible in C++.[1]
  12. ^ a b c d The keyword here is not a value in itself and it can only be used to access a method of the superclass.
  13. ^ a b In this language, instance methods are passed the current object as the first parameter, which is conventionally named "self", but this is not required to be the case.
  14. ^ "super" in Ruby, unlike in other languages, is actually a call to the method of the same name in the superclass. So super(args) in Ruby is equivalent to "super.currentMethodName(args)" in Java. There is no way of calling a method of different name in the superclass.
  15. ^ In OCaml, an object declaration can optionally start with a parameter which will be associated with the current object. This parameter is conventionally named "self", but this is not required to be the case. It is good practice to put a parameter there so that one can call one's own methods.
  16. ^ In OCaml, an inheritance declaration ("inherit") can optionally be be associated with a value, with the syntax "inherit parent_class «parameters» as super". Here "super" is the name we gave to the variable associated with this parent object. It can be named something else.
  17. ^ a b c Upcasting is implicit in this language. A subtype instance can be used where a supertype is needed.
  18. ^ a b This is a dynamically-typed language. Casting between types is unnecessary.
  19. ^ This language has no run-time type information. It is unnecessary as it is statically typed and downcasting is not possible.

This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.


Giant Panda

Mercedes Car
James Bond Guide
This site monitored by SitePinger.net