Lesson 23 / 25
OOP Across Languages
Compare how Java, C++, Python, JavaScript, Go and Rust support object-oriented ideas.
Same ideas, different mechanisms
Languages implement OOP differently. Java and C#: class-based, single class inheritance, multiple interfaces, garbage collection, every method virtual by default in Java (C# requires virtual). C++: classes with multiple inheritance, virtual functions for dynamic dispatch, deterministic destructors and RAII, value and reference semantics both available. Python: everything is an object, multiple inheritance with a method resolution order, duck typing, no enforced privacy, special methods for operators, and dataclasses for value types. JavaScript: prototype-based inheritance; the class keyword (ES2015) is syntax over prototypes, with private fields using #. Go: no classes or inheritance; structs with methods, embedding for composition and implicit, structural interfaces. Rust: structs and enums with methods, traits for shared behaviour (with static or dynamic dispatch through dyn Trait), no implementation inheritance. Kotlin and Swift favour final-by-default classes and value types. Understanding the principles lets you apply them in any of these languages.
Polymorphism in Python and Go
Both achieve "one interface, many implementations" without Java-style declarations.
# Python: duck typing plus an abstract base class for documentation and checks
from abc import ABC, abstractmethod
import math
class Shape(ABC):
@abstractmethod
def area(self) -> float: ...
class Circle(Shape):
def __init__(self, r: float):
self.r = r
def area(self) -> float:
return math.pi * self.r ** 2
class Square(Shape):
def __init__(self, side: float):
self.side = side
def area(self) -> float:
return self.side ** 2
total = sum(s.area() for s in [Circle(1), Square(2)])
# Go: structural interfaces, no 'implements' keyword
# type Shape interface { Area() float64 }
# type Square struct { Side float64 }
# func (s Square) Area() float64 { return s.Side * s.Side } // Square satisfies Shape implicitlyDifferent scripts, same language of ideas
Hindi can be written in Devanagari or transliterated in Latin letters; the meaning stays the same. OOP principles are the meaning, and each programming language is a different script for expressing them.
Quick check: Which language implements interfaces structurally, so a type satisfies an interface just by having its methods?
- Java
- Go
- C#
- C++
Answer
Go — Go interfaces are satisfied implicitly by any type with the required method set.