Comparison of programming languages (object-oriented programming)
{{Multiple issues|
{{cleanup|reason=This article's reference section contains many footnotes, but lists no external references or sources.|date=June 2013}}
{{More citations needed|date=January 2025}}
}}
{{ProgLangCompare}}
This comparison of programming languages compares how object-oriented programming languages such as C++, Java, Smalltalk, Object Pascal, Perl, Python, and others manipulate data structures.
__TOC__
{{-}}
Object construction and destruction
class="wikitable" |
! construction
! destruction |
---|
ABAP Objects
| |SAP reserved to himself the use of destructionThis language uses garbage collection to release unused memory. |
APL (Dyalog)
| | |
C++
| | |
C#
| rowspan=3| |
Java |
D
| |
eC
| | |
Objective-C (Cocoa)
| | |
Swift
| | |
Python
| |
Visual Basic .NET
| |
Xojo
| | |
Eiffel
| |
PHP
| |
Perl 5
| | |
Raku
| | |
Ruby
| |
Windows PowerShell
| | |
OCaml
| |
F#
| |
Smalltalk
| The class is an Object. Point x: 10 y: 20. Array with: -1 with: 3 with: 2. | |
JavaScript
| |
{{nowrap|Object Pascal}} (Delphi)
| | |
Scala
| val obj = new Object // no parameters val obj = new Object(arg0, arg1, arg2...) val obj = Object(arg0, arg1, arg2...) // case class val obj = new Object(arg0, arg1, param1 = value1, ...) // named parameters |
COBOL
| | |
Cobra
| | |
ISLISP
| |
Class declaration
class="wikitable" |
! class
! protocol |
---|
ABAP Objects
| | | {{n/a}} |
APL (Dyalog)
| | | |
C++
| | | rowspan=2| |
C#
| rowspan=2| | rowspan=2| |
D
| |
eC
| | | |
Java
| rowspan=2| | rowspan=2| | |
PHP
| |
Objective-C
| | | {{n/a|Prefixes to class and protocol names conventionally used as a kind of namespace}} |
Swift
| | | |
Python
| |In Python interfaces are classes which methods have pass as their bodies | |
Visual Basic .NET
| | | |
Xojo
| | | |
Eiffel
| | colspan=2 {{n/a}} |
Perl
| | | |
Raku
| | | |
Ruby
| | | |
Windows PowerShell
| colspan=3 {{n/a}} |
OCaml
| | | |
F#
| | | |
Smalltalk
| |The namespace is an Object. |
JavaScript (ES6)
| | | |
Object Pascal (Delphi)
| private // Private members(include Methods and Fields) public // Public members protected // Protected members published // Published members end; | | |
Scala
| class ConcreteClass(constructor params) extends ParentClass with Trait1 with Trait2 with Trait2 { // members } | trait TraitName extends OtherTrait1 with OtherTrait2 with OtherTrait3 { // members } | package name |
COBOL
| FACTORY« IMPLEMENTS interfaces». class-members END FACTORY. OBJECT« IMPLEMENTS interfaces». instance-members END OBJECT.
| members
| {{n/a}} |
Cobra
| | | |
ISLISP
| | | |
Class members
= Constructors and destructors =
class="wikitable" | |
! constructor
! finalizerA finalizer 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. | |
---|---|
ABAP Objects
| | colspan=2 {{n/a}} | |
APL (Dyalog)
| | | | |
C++
| : 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 : If an initializer is not specified for a member or parent class, then the default constructor is used. | | | |
C#
| | | | |
D
| | | | |
eC
| | | | |
Java
| | | | |
Eiffel
|Any Eiffel procedure can be used as a creation procedure, aka constructors. See Eiffel paragraph at Constructor (computer science). | | |
Objective-C (Cocoa)
| | | | |
Swift
| | | | |
Python
| | | | |
Visual Basic .NET
| | | | |
Xojo
| | | | |
PHP
| | | | |
Perl
| | | | |
Raku
| | | | |
Ruby
| | colspan=2 {{n/a}} | |
Windows PowerShell
| colspan=3 {{n/a}} | |
OCaml
| colspan=2 {{n/a}} | |
F#
| | | | |
JavaScript
| | colspan=2 {{n/a}} | |
JavaScript (ES6)
| | |
COBOL
| {{n/a}}Constructors can be emulated with a factory method returning a class instance. | colspan=2 {{n/a}} | |
Cobra
| | | | |
ISLISP
| |
= Fields =
class="wikitable" |
! public
! private ! protected ! friend |
---|
ABAP Objects
| | |
APL (Dyalog)
| | | | |
C++
| | | |In C++, specific fields are not declared as accessible by outside things. Rather, outside functions and classes are declared as friends to have access to the class's fields. See friend function and friend class for more details. |
C#
| rowspan=3| | rowspan=3| | | |
D
| | |
Java
| | |
eC
| | |
Eiffel
| | | | |
Objective-C
| | | | |
Swift
| colspan=4 {{n/a}} |
Smalltalk
| colspan=2 {{n/a}} |Just send a message to the class class addInstVarName: field. class removeInstVarName: field. | {{n/a}} |
Python
| | colspan=2 {{n/a}} |
valign="top"
| Visual Basic .NET | | | | |
valign="top"
| Xojo | | | | {{n/a}} |
PHP
| | | | |
Perl
| colspan=3 {{n/a}} |
Raku
| | | colspan=2 {{n/a}} |
Ruby
| colspan=2 {{n/a}} | | |
Windows PowerShell
| | colspan=3 {{n/a}} |
OCaml
| colspan=2 {{n/a}} | | rowspan=2 {{n/a}} |
F#
| {{n/a}} | | {{n/a}} |
JavaScript
| | | | |
COBOL
| {{n/a}} | level-number field clauses.All class data is 'private' because the COBOL standard does not specify any way to access it. | {{n/a}} | {{n/a}} |
Cobra
| | | | |
ISLISP
| | | | |
= Methods =
class="wikitable" |
valign="top"
! ! basic/void method ! value-returning method |
valign="top"
| ABAP Objects | |
valign="top"
| APL (Dyalog) | | |
valign="top"
| C++In C++, declaring and implementing methods is usually separate. Methods are declared in the class definition (which is usually included in a header file) using the syntax : The implementation of methods is usually provided in a separate source file, with the following syntax : Although the body of a method can be included with the declaration inside the class definition, as shown in the table here, this is generally bad practice. Because the class definition must be included with every source file which uses the fields or methods of the class, having code in the class definition causes the method code to be compiled with every source file, increasing the size of the code. Yet, in some circumstances, it is useful to include the body of a method with the declaration. One reason is that the compiler will try to inline methods that are included in the class declaration; so if a very short one-line method occurs, it may make it faster to allow a compiler to inline it, by including the body along with the declaration. Also, if a template class or method occurs, then all the code must be included with the declaration, because only with the code can the template be instantiated. | rowspan=4| | rowspan=4| |
valign="top"
| C# |
valign="top"
| D |
valign="top"
| Java |
valign="top"
| eC | | |
valign="top"
| Eiffel | | |
valign="top"
| Objective-C | | |
valign="top"
| Swift | | |
valign="top"
| Python | | |
valign="top"
| Visual Basic .NET | | |
valign="top"
| Xojo | | |
valign="top"
| PHP | | |
valign="top"
| Perl | | |
valign="top"
| Raku | | |
valign="top"
| Ruby | | |
valign="top"
| Windows PowerShell | | |
valign="top"
| OCaml | rowspan=2 {{n/a}} | |
valign="top"
| F# | |
valign="top"
| JavaScript | | |
Javascript (ES6)
| | |
valign="top"
| COBOL | instructions
| instructions
|
Cobra
| | | |
ISLISP
| | |
= Properties =
How to declare a property named "Bar"
== Manually implemented ==
class="wikitable" |
valign="top"
! ! read-write ! read-only ! write-only |
valign="top"
| ABAP Objects | colspan=3 {{n/a}} |
valign="top"
| APL (Dyalog) | | | |
valign="top"
| C++ | colspan=3 {{n/a}} |
valign="top"
| C# | | | |
valign="top"
| D | | | |
valign="top"
| eC | | | |
valign="top"
| Java | colspan=3 {{n/a}} |
valign="top"
| Objective-C 2.0 (Cocoa) | | | {{n/a}} |
valign="top"
| Swift | | | {{n/a}} |
valign="top"
| Eiffel | | | |
valign="top"
| Python | def bar(): doc = "The bar property." def fget(self): return self._bar def fset(self, value): self._bar = value return locals() bar = property(**bar()) | | |
valign="top"
| Visual Basic .NET | | | |
valign="top"
| Xojo | | | |
valign="top"
| PHP | | | |
valign="top"
| Perl | | | |
valign="top"
| Raku | colspan=3 {{n/a}} |
valign="top"
| Ruby | | | |
valign="top"
| Windows PowerShell | | | |
valign="top"
| OCaml | colspan=3 {{n/a}} |
valign="top"
| F# | | | |
JavaScript (ES6)
| | | |
valign="top"
| COBOL | instructions
instructions
| instructions
| instructions
|
Cobra
| | | |
valign="top"
| ISLISP | colspan=3 {{n/a}} |
== Automatically implemented ==
class="wikitable" |
valign="top"
! ! read-write ! read-only ! write-only |
valign="top"
| ABAP Objects | colspan=3 {{n/a}} |
valign="top"
| C++ | colspan=3 {{n/a}} |
valign="top"
| C# | | | |
valign="top"
| D | colspan=3 {{n/a}} |
valign="top"
| Java | colspan=3 {{n/a}} |
valign="top"
| Objective-C 2.0 (Cocoa) | | | {{n/a}} |
valign="top"
| Swift | | | {{n/a}} |
valign="top"
| Eiffel | | | |
valign="top"
| Python | | | |
valign="top"
| Visual Basic .NET | | | |
valign="top"
| PHP | | | |
valign="top"
| | | |
valign="top"
| Raku | colspan=3 {{n/a}} |
valign="top"
| Ruby | | | |
valign="top"
| Windows PowerShell | | | |
valign="top"
| OCaml | colspan=3 {{n/a}} |
valign="top"
| F# | | | |
valign="top"
| COBOL | | | |
Cobra
| | | |
= Overloaded operators =
== Standard operators ==
class="wikitable"
| ! unary ! binary ! function call |
ABAP Objects
| colspan=3 {{n/a}} |
C++
| | | |
C#
| | | {{n/a}} |
D
| | | |
Java
| rowspan=2 colspan=3 {{n/a}} |
Objective-C |
Swift
| | | |
EiffelAlthough Eiffel does not support overloading of operators, it can define operators
| | | |
Python
| | | |
Visual Basic .NET
| | | {{n/a}} |
Xojo
| | colspan=2 {{n/a}} |
PHP
| colspan=2|PHP does not support operator overloading natively, but support can be added using the [http://pecl.php.net/package/operator "operator" PECL package]. | |
Perl
| | | |
Raku
| | | |
Ruby
| | | {{n/a}} |
Windows PowerShell
| rowspan=2 colspan=3 {{n/a}} |
OCaml |
F#
| | | {{n/a}} |
COBOL
| colspan=3 {{n/a}} |
ISLISP
| colspan=3 {{n/a}} |
== Indexers ==
class="wikitable"
| ! read-write ! read-only ! write-only |
valign="top"
| ABAP Objects | colspan=3 {{n/a}} |
valign="top"
| APL (Dyalog) | | | |
valign="top"
| C++ | | | |
valign="top"
| C# | | | |
valign="top"
| D | | | |
valign="top"
| Java | colspan=3 {{n/a}} |
valign="top"
| Objective-C (recent Clang compiler) | {{n/a}} | | |
valign="top"
| Swift | | | |
valign="top"
| | | |
valign="top"
| Python | | | |
valign="top"
| Visual Basic .NET | | | |
valign="top"
| PHP |
valign="top"
| Perl |
valign="top"
| Raku | | | {{n/a}} |
valign="top"
| Ruby | | | |
valign="top"
| Windows PowerShell | rowspan=2 colspan=3 {{n/a}} |
valign="top"
| OCaml |
valign="top"
| F# | | | |
valign="top"
| COBOL | colspan=3 {{n/a}} |
Cobra
| | | |
== Type casts ==
Member access
How to access members of an object x
class="wikitable" |
! colspan=3| object member
! rowspan=2| class member ! rowspan=2| namespace member |
---|
! method
! field ! property |
ABAP Objects
| : : | | {{n/a}} | | {{n/a}} |
C++
| | | | | |
Objective-C
| | | | | |
Smalltalk
| | {{n/a}} | | | |
Swift
| | | | | |
APL (Dyalog)
| | rowspan=8| | | rowspan=7| | rowspan=9| |
C#
| rowspan=8| |
Java
| {{n/a}} |
D
| rowspan=6| |
Python |
Visual Basic .NET |
Xojo |
Windows PowerShell
| |
F#
| {{n/a}} | |
eC
| | | | | |
Eiffel
| rowspan=2| | | | | rowspan=2 {{n/a}} |
Ruby
| {{n/a}} | | |
PHP
| | | | | |
Perl
| | | | | |
Raku
| | | | | |
OCaml
| | colspan=2 {{n/a}} | | |
JavaScript
| | | | | {{n/a}} |
COBOL
| | {{n/a}} | | | {{n/a}} |
Cobra
| | | | | |
Member availability
class="wikitable" |
! colspan=2| Has member?
! colspan=2| Handler for missing member |
---|
! Method
! Field ! Method ! Field |
APL (Dyalog)
| | | colspan=2 {{n/a}} |
ABAP Objects
| rowspan=2 colspan=4 {{n/a}} |
C++ |
Objective-C (Cocoa)
| | {{n/a}} | | {{n/a}} |
Smalltalk
| | {{n/a}} | | {{n/a}} |
C#
| rowspan=3 colspan=4| (using reflection) |
eC |
Java |
D
| | | colspan=2| |
Eiffel
| colspan=4 {{n/a}} |
Python
| | | colspan=2 | |
Visual Basic .NET
| colspan=4 |(using reflection) |
Xojo
| colspan=4 |(using Introspection) |
Windows PowerShell
| colspan=4 |(using reflection) |
F#
| colspan=4 |(using reflection) |
Ruby
| | {{n/a}} | | {{n/a}} |
PHP
| | | | |
Perl
| | | AUTOLOAD | |
Raku
| | | AUTOLOAD | |
OCaml
| colspan=4 {{n/a}} |
JavaScript
| | | | |
COBOL
| colspan=4 {{n/a}} |
Special variables
class="wikitable" |
! current object |
---|
Smalltalk
| | | | |
ABAP Objects
| | | | |
APL (Dyalog)
| | | | |
C++
| | | |
C#
| rowspan=4| | | rowspan=3| | |
Java
| |
D
| |
JavaScript
| | |
eC
| | | | |
Objective-C
| | | |
Swift
| | |
Python
| | | |
Visual Basic .NET
| | | | |
Xojo
| | | | |
Eiffel
| | | | |
PHP
| | | |
Perl
| | |
Raku
| | | | |
Ruby
| | | |
Windows PowerShell
| | | | |
OCaml
| |
F#
| | | |
COBOL
| | | | |
Cobra
| | | | |
Special methods
class="wikitable" |
rowspan=2|
!colspan=2| String representation !rowspan=2| Object copy !rowspan=2| Value equality !rowspan=2| Object comparison !rowspan=2| Hash code !rowspan=2| Object ID |
---|
Human-readable
! Source-compatible |
ABAP Objects
| colspan=7 {{n/a}} |
APL (Dyalog)
| | | | | colspan=2 {{n/a}} |
C++
| | | | | | pointer to object can be converted into an integer ID |
C#
| | | | | | | |
Java
| | | | | | | |
JavaScript
| | | | | | | |
D
| | | | | | | |
eC
| | | | | | | object handle can be converted into an integer ID |
Objective-C (Cocoa)
| | | | | | | pointer to object can be converted into an integer ID |
Swift
| | | | | | | |
Smalltalk
| | | | | | | |
Python
| | | | | |
Visual Basic .NET
| | | | | | | |
Eiffel
| | | | | When x is | When x is | When x is |
PHP
| | | | | | | |
Perl
| | | | | | | |
Raku
| | | | | | |
Ruby
| | | | | | | |
Windows PowerShell
| | | | | | | |
OCaml
| | | | | | | |
F#
| | | | | | | |
COBOL
| colspan=7 {{n/a}} |
Type manipulation
class="wikitable" |
rowspan=2|
! rowspan=2| Get object type ! rowspan=2| Is instance of (includes subtypes) ! rowspan=2| Upcasting ! colspan=2| Downcasting |
---|
Runtime check
! No check |
ABAP Objects
| colspan=2 {{n/a}}Run-time type information in ABAP can be gathered by using different description Classes like CL_ABAP_CLASSDESCR. | = | ?= |
C++
| | | rowspan=8 {{n/a}}Upcasting is implicit in this language. A subtype instance can be used where a supertype is needed. | | |
C#
| | | | |
D
| | | | |
Delphi
| | | | |
eC
| | | | |
Java
| | | | |
Objective-C (Cocoa)
| | | |
Swift
| | | |
JavaScript
| | | colspan=3 {{n/a}}This language is dynamically typed. Casting between types is unneeded. |
Visual Basic .NET
| | | | |
Xojo
| | | {{n/a}} | | {{n/a}} |
Eiffel
| | | | |
Python
| | |
PHP
| | |
Perl
| | |
Raku
| | | | |
Ruby
| | |
Smalltalk
| | |
Windows PowerShell
| | | | |
OCaml
| colspan=2 {{n/a}}This language doesn't give run-time type information. It is unneeded because it is statically typed and downcasting is impossible. | rowspan=2| | colspan=2 {{n/a}} |
F#
| | | | |
COBOL
| colspan=2 {{n/a}} | colspan=2 {{n/a}} |
Namespace management
class="wikitable" |
rowspan=2|
! colspan=2| Import namespace ! rowspan=2| Import item |
---|
qualified
! unqualified |
ABAP Objects
| | | |
C++
| | | |
C#
| | | |
D
| | | |
Java
| | | |
Objective-C
| | | |
Visual Basic .NET
| | | |
Eiffel
| | | |
Python
| | | |
PHP
| | | |
Perl
| rowspan=2| | | |
Raku
| | |
Ruby
| | | |
Windows PowerShell
| | | |
OCaml
| | rowspan=2| | |
F#
| | |
COBOL
| colspan=3 {{n/a}} |
Contracts
class="wikitable" |
! Precondition
! Postcondition ! Check ! Invariant ! Loop |
---|
ABAP Objects
| colspan=5 rowspan=2 {{n/a}} |
C++ |
C#
| | |
Java
| colspan=5 rowspan=3 {{n/a}} |
Objective-C |
Visual Basic .NET |
D
| | | | | |
Eiffel
| | | | | |
Python
| colspan=5 rowspan=3 {{n/a}} |
PHP |
Perl |
Raku
| | | | | |
Ruby
| colspan=5 rowspan=5 {{n/a}} |
Windows PowerShell |
OCaml |
F# |
COBOL |
See also
Notes
{{Reflist|2}}