9.1. Fundamentals

Although not widely used until the turn of the century, object-oriented programming's roots date back several decades to languages such as Simula and Smalltalk. OO programming has been the subject of much academic research and debate, especially since the widespread adoption of OO programming languages by practicing developers.

9.1.1. Classes and Objects

There are many different ways and no clear consensus on how to describe and define object orientation as a programming technique, but all of them revolve around the notions of classes and objects. A class is an abstract definition of something that has attributes (sometimes called properties or states) and actions (capabilities or methods). An object is a specific instance of a class that has its own state separate from any other object instance. Here's a class definition for Point, which is a pair of integers that represents the x and y values of a point in a Cartesian coordinate system:

public class Point {
    private int x;
    private int y;

    public Point( int x, int y ){
        this.x = x;
        this.y = y;

    public Point( Point other ){
        x = other.getX();
        y = other.getY();

    public int getX(){ return x; }

    public int getY(){ return y; }

    public Point relativeTo( int dx, int dy ){
        return new Point( x + dx, y + dy );

    public String toString(){
        StringBuffer b = new StringBuffer();
        b.append( '(' );
        b.append( x );
        b.append( ',' );
        b.append( y );
        b.append( ')' );

        return b.toString();

To represent a specific point, simply create ...

Get Programming Interviews Exposed: Secrets to Landing Your Next Job, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.