Object-Oriented Programming
Class Anatomy
Classes and Objects
Think of a class as a collection of methods, or as a cookie cutter. Each cookie you cut is an object — an instance of the class — made from the same template. Every object has the same attributes and methods as the class that made it, but each object's attribute values can differ, the way two cookies from the same cutter can be sprinkled with different candy.
The Four Pillars
OOP is usually summarized as four principles — implemented, à la carte, by languages like Java, Scala, Python, and Ruby:
| Pillar | What it means |
|---|---|
| Abstraction | Define abstract classes and interfaces — a shape without every detail filled in. |
| Encapsulation | Define public and private members, so a class controls its own internal state. |
| Inheritance | One class can inherit properties and methods from a parent class. |
| Polymorphism | A method from a parent class can be overridden in a child class. |
Abstraction
Most languages implement abstraction with classes: a named block of code that defines a scope. Inside that scope you define data (fields) and functions (methods) — together these are a class's members.
Members are more or less protected: public members are visible from outside the class, private members only from inside it. A class's methods can likewise be public or private.
Sometimes a method is declared but only partially implemented — an abstract method. A class containing an abstract method is itself abstract, and cannot be used directly until a sub-class fully implements it (see Inheritance, below).
Encapsulation
Encapsulation wraps data (fields) and the code that acts on it (methods) into one unit. Declaring a class's fields private strengthens encapsulation — nothing outside the class can reach into its state directly.
A class is instantiated through a special method called a constructor, which produces an object. Each object encapsulates its own copy of the data.
A class can also define static methods or fields — members of the class itself rather than of any one object, usable without creating an instance. Static fields typically represent class-wide settings or shared state, not per-object data.
Inheritance
Inheritance lets you define one class in terms of another: a class can extend another class, inheriting its members. Every class in such a hierarchy ultimately derives from some root class.
Polymorphism
When a derived class inherits a method, it can override that method to replace the parent's default behavior — extending and modifying the class to create something with similar but not identical behavior.
The payoff: you can hold a reference to an object typed as its parent class, call a method on it, and get the object's own method, not the parent's. The method call becomes polymorphic — it changes depending on the actual object, not the type of the reference.
Advanced Features
Beyond the four pillars, OOP languages differ in the extra tools they give you for writing reusable, shareable code:
Generics
A generic class is not tied to one specific data type — a dynamic list has the same logic whether it holds integers, floats, or strings. Generics relax strong typing's cost: instead of writing one list class per type, you write one generic list and specialize it per use. Dynamically typed languages don't need this mechanism, since any variable can already hold any type.
Interfaces
An interface is close to pure abstraction: it enforces a shape of behavior without an implementation. The key difference from an abstract class is that a class can implement multiple interfaces but extend only one abstract class. Older Java required every interface method to be abstract; modern Java allows default implementations, though most developers still treat interfaces as pure contracts.
Mix-ins and Traits
Multiple inheritance — inheriting from more than one base class — is unsupported by most mainstream OOP languages (Java, Scala, Ruby included). Instead, a class can mix in several traits (called modules in Ruby): reusable namespaces of methods and variables, combined into one class without the ambiguity multiple inheritance introduces.
Representative Languages
These five languages show OOP from its purest form to its most widely used one.
| Language | Why study it |
|---|---|
| Smalltalk | The origin: everything is an object and every action is a message. |
| Self | Objects without classes: prototypes, slots and delegation. |
| Eiffel | Design by contract: preconditions, postconditions and invariants in every class. |
| Ruby | Every value is an object; blocks, mixins and open classes make it expressive. |
| Java | Class-based OOP at industrial scale, with interfaces and a portable virtual machine. |