Introduction to Interactive Programming
by Lynn Andrea Stein
A Rethinking CS101 Project

Java Rules

A Class...

...is a factory for producing instance objects.

...may be final, in which case it cannot be subclassed (extended).
...may be abstract, in which case it cannot be instantiated (using new).
...may be public, in which case it is visible in all packages, or default, in which case it is only visible within its package.

...name is a Java type.

...extends exactly 1 other class. (If none is specified explicitly, the class extends Object.)
...implements 0 or more interfaces.

...has a body enclosed in {}s.

A class body contains fields and methods ONLY, not, e.g., freestanding code.*

A field...

...may be public, protected, private protected, private, or default (none of the above).
...may be static, in which case it belongs to the class (factory), rather than to a particluar instance. If a field is not static, each instance of the class has its own copy of the field.
...may be final, in which case it must be initialized and its value cannot change.
...may be transient and/or volatile. (These properties are outside the scope of this course.)

...must be declared.
...may be initialized (defined).

A method...

...may be public, protected, private protected, private, or default (none of the above).
...may be abstract, provided that its definition occurs in an abstract class. In this case, the declaration ends with a ; and no {}-enclosed body is given.
...may be static, in which case it belongs to the class (factory), rather than to a particular instance.
...may be final, in which case it cannot be overriden in subclasses.
...may be synchronized, in which case an exclusive lock is obtained on its containing object before the method body is executed.
...may be native, in which case it is typically implemented in a language other than Java (and hence outside the scope of this course).

...must have a return type, which is either a legitimate Java type or void, unless it is a constructor method.
...must have a name. If it is a constructor method, this name must exactly match the class name.
...must have a (possibly empty) list of arguments, enclosed in parentheses and separated by commas.
...may throw 0 or more exceptions, which should be declared (unless they are RuntimeExceptions).

...must have a body, enclosed in {}, unless the method is declared abstract.

Disclaimers, notes, amendments, etc.

*Truth in advertising: A class body contains fields and methods ONLY, no freestanding code, except for static initializers, which are outside the scope of this course.


This course is a part of Lynn Andrea Stein's Rethinking CS101 project at the MIT AI Lab and the Department of Electrical Engineering and Computer Science at the Massachusetts Institute of Technology.