Classes and Objects compared across 16 programming languages
Object-oriented programming: classes, objects, and inheritance – or each language's equivalent.
PyPython
class Tier:
def __init__(self, name):
self.name = name
def sprich(self):
return f"{self.name} makes a sound"
class Hund(Tier):
def sprich(self):
return f"{self.name} says Woof!"
print(Hund("Rex").sprich())Hund inherits from Tier and overrides sprich.
JSJavaScript
class Tier {
constructor(name) {
this.name = name;
}
sprich() {
return this.name + " makes a sound";
}
}
class Hund extends Tier {
sprich() {
return this.name + " says Woof!";
}
}
console.log(new Hund("Rex").sprich());Hund inherits from Tier and overrides sprich; behind the scenes the prototype chain is still at work.
TSTypeScript
class Tier {
constructor(protected name: string) {}
sprich(): string {
return this.name + " makes a sound";
}
}
class Hund extends Tier {
override sprich(): string {
return this.name + " says Woof!";
}
}
console.log(new Hund("Rex").sprich());protected name in the constructor creates the field (parameter property). override makes sure the base class really has the method.
JvJava
public class Main {
static class Tier {
protected final String name;
Tier(String name) { this.name = name; }
String sprich() { return name + " makes a sound"; }
}
static class Hund extends Tier {
Hund(String name) { super(name); }
@Override String sprich() { return name + " says Woof!"; }
}
public static void main(String[] args) {
System.out.println(new Hund("Rex").sprich());
}
}Hund inherits from Tier (extends) and overrides sprich. super(name) calls the constructor of the base class.
CC
#include <stdio.h>
typedef struct { char name[20]; int alter; } Person;
void geburtstag(Person *p) { p->alter++; }
int main(void) {
Person ada = {"Ada", 36};
geburtstag(&ada);
printf("%s is %d\n", ada.name, ada.alter);
return 0;
}C has no classes: you use a struct for the data and functions that receive a pointer to it as their first parameter (p->alter). This replaces self/this; there is no inheritance.
C++C++
#include <iostream>
#include <string>
struct Tier {
std::string name;
virtual ~Tier() = default;
virtual std::string sprich() const { return name + " makes a sound"; }
};
struct Hund : Tier {
std::string sprich() const override { return name + " says Woof!"; }
};
int main() {
Hund h; h.name = "Rex";
const Tier& t = h; // access via the base: virtual selects Hund::sprich
std::cout << t.sprich() << '\n';
}struct is a class with public members. virtual + override enable polymorphism; via the base reference, Hund::sprich is called. The virtual destructor is mandatory for base classes.
C#C#
Console.WriteLine(new Hund("Rex").Sprich());
class Tier(string name)
{
public string Name { get; } = name;
public virtual string Sprich() => $"{Name} makes a sound";
}
class Hund(string name) : Tier(name)
{
public override string Sprich() => $"{Name} says Woof!";
}Hund inherits from Tier and overrides the virtual method with override. The primary constructors (C# 12) save the classic constructor.
GoGo
package main
import "fmt"
type Tier struct{ Name string }
func (t Tier) Sprich() string { return t.Name + " makes a sound" }
type Hund struct{ Tier }
func (h Hund) Sprich() string { return h.Name + " says Woof!" }
func main() {
fmt.Println(Hund{Tier{"Rex"}}.Sprich())
}Go has no classes: a struct holds the data, and methods attach via the receiver. Embedding (Tier in Hund) replaces inheritance – Hund gets the field Name and overrides Sprich.
RsRust
trait Tier {
fn name(&self) -> &str;
fn sprich(&self) -> String { format!("{} makes a sound", self.name()) }
}
struct Hund { name: String }
impl Tier for Hund {
fn name(&self) -> &str { &self.name }
fn sprich(&self) -> String { format!("{} says Woof!", self.name) }
}
fn main() {
let rex = Hund { name: "Rex".to_string() };
println!("{}", rex.sprich());
}Rust has no classes and no inheritance: data lives in the struct, methods in the impl, and shared behavior (including a default implementation) in a trait. Without a trait, struct plus impl is enough.
PHPPHP
<?php
class Tier {
public function __construct(protected string $name) {}
public function sprich(): string {
return "{$this->name} makes a sound";
}
}
class Hund extends Tier {
public function sprich(): string {
return "{$this->name} says Woof!";
}
}
echo (new Hund("Rex"))->sprich(), "\n";Constructor promotion (protected string $name) declares and sets the property in one step. Hund inherits from Tier and overrides sprich.
RbRuby
class Tier
def initialize(name)
@name = name
end
def sprich
"#{@name} makes a sound"
end
end
class Hund < Tier
def sprich
"#{@name} says Woof!"
end
end
puts Hund.new("Rex").sprichinitialize is the constructor, @name an instance variable. Hund < Tier means inheritance; sprich is overridden.
SwSwift
class Tier {
let name: String
init(name: String) { self.name = name }
func sprich() -> String { "\(name) makes a sound" }
}
class Hund: Tier {
override func sprich() -> String { "\(name) says Woof!" }
}
print(Hund(name: "Rex").sprich())Hund inherits from Tier and overrides sprich with override. For pure data, a struct (value type) is usually better.
KtKotlin
open class Tier(val name: String) {
open fun sprich() = "$name makes a sound"
}
class Hund(name: String) : Tier(name) {
override fun sprich() = "$name says Woof!"
}
fun main() {
println(Hund("Rex").sprich())
}Classes and methods are final by default; open allows inheritance, override overrides. The property name is created directly in the constructor.
SQLSQL
CREATE TABLE bewertungen (
id INTEGER PRIMARY KEY,
kunde_id INTEGER NOT NULL REFERENCES kunden(id) ON DELETE CASCADE,
produkt_id INTEGER NOT NULL REFERENCES produkte(id),
sterne INTEGER NOT NULL CHECK (sterne BETWEEN 1 AND 5),
kommentar TEXT,
UNIQUE (kunde_id, produkt_id)
);A table corresponds to a class: columns are attributes with a type, rows are instances. Constraints (NOT NULL, CHECK, UNIQUE, FOREIGN KEY) are the invariants the database guarantees for every row. There is no inheritance; relationships work through foreign keys.
shBash
# Bash has no classes: prefix namespace + associative array
declare -A konto
konto_init() { konto[besitzer]="$1"; konto[saldo]=0; }
konto_einzahlen() { konto[saldo]=$(( konto[saldo] + $1 )); }
konto_zeige() { echo "${konto[besitzer]}: ${konto[saldo]} EUR"; }
konto_init "Ada"
konto_einzahlen 100
konto_einzahlen 50
konto_zeigeBash has no classes or inheritance. The closest substitute: data in an associative array (Bash 4+) and functions with a common prefix. For serious object models, Bash is the wrong language.
</>HTML & CSS
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.btn { display: inline-block; padding: .6rem 1.2rem; border: 2px solid transparent; border-radius: 8px;
font: inherit; font-weight: 600; cursor: pointer; background: #ddd; color: #222; }
.btn--primaer { background: #2b59c3; color: #fff; }
.btn--gefahr { background: #c62828; color: #fff; }
.btn--outline { background: transparent; border-color: #2b59c3; color: #2b59c3; }
.btn:hover { filter: brightness(.92); }
.btn--klein { padding: .3rem .7rem; font-size: .85rem; }
</style>
<button class="btn">Default</button>
<button class="btn btn--primaer">Primary</button>
<button class="btn btn--gefahr">Delete</button>
<button class="btn btn--outline btn--klein">Small outline</button>A base class (.btn) defines what is shared, modifiers (.btn--primaer) add variants – this is the BEM naming convention (Block, Element, Modifier). Classes are the "class/inheritance principle" of CSS.