Java O-O Concepts
Fall 2011, David Matuszek

In this paper I've tried to summarize all the object-oriented concepts used in Java--please let me know about any errors or omissions. Note that it is not enough to memorize these ideas; you must understand them and be able to apply them.

Main concept: Object-oriented programming is all about creating a society of cooperating, active "agents" (objects) that, working together, accomplish the desired task.

Classes and objects

A class describes the data and the methods of its objects (also called instances). Every object belongs to some class.

An object contains data (instance variables) representing its state, and instance methods, which are the things it can do. Each object has its own copies of the instance variables.

A class may also contain its own data (class variables) and class methods. The keyword static denotes such data and methods. There is only one copy of each static variable. Methods should be static if they don't access any instance variables or instance methods.

Instance variables and static variables may be given an initial value when they are declared. Uninitialized numeric types are set to zero, boolean variables are set to false, char variables are set to '\0', and object variables are set to null.

An object "knows" what class it belongs to, and can use class data and class methods, but a class does not "know" about its objects.

Classes form a hierarchy (tree), with Object at the root. Every class, except Object, has one and only one immediate superclass, but that class has its own immediate superclass, and so on all the way up to Object at the root, and all of these are superclasses of the class. The keyword extends denotes the immediate superclass. Class B should extend class A if and only if the things represented by class B really are a more specific kind of A.

A class contains one or more constructors for making new objects of that class. If (and only if) the programmer does not write a constructor, Java provides a default constructor with no arguments.

The purpose of a constructor is to create an object in a valid state. No other work should be done in a constructor.

When a constructor executes, the very first thing it does is call the constructor for its superclass. You can write this constructor call explicitly, with super(...); as the first thing done in the constructor, or you can let it implicitly call the default constructor. However, you must explicitly call a superclass constructor if the superclass has no default constructor and all its explicitly defined constructors require arguments.

A constructor for a class can call another constructor for the same class by putting this(...); as the first thing done in the constructor. This helps you avoid repeating code.

Variables to hold objects are declared just like variables to hold primitives, with the syntax Type variable;. Declaring an object variable allocates space for a reference to an object, but does not create an object; the reference is null until some object is assigned to the variable. An object can be created by calling the corresponding constructor (using the keyword new).

Classes inherit (have available) all the data and all the methods of their superclasses, but do not inherit constructors.

You can assign an object to a variable (that is, variable = object) if the variable is declared to be of that class or any of its superclasses (thus, you can assign any object to a variable of type Object). If you have an object in a more general variable or expression (for example, a String value in an Object variable), it retains all its additional variables and methods, but they are not accessible. To make them accessible, you can cast the object to the correct type with the syntax (type)variable or (type)(expression).

Casting an object to a more general type is called upcasting, and is always legal. Casting an object to a more specific type is called downcasting, and Java inserts a run-time check to ensure that the cast is legal. Casting does not affect what the object is, it only changes what fields and methods are available on the object at the position the cast occurs.

A class that is declared as final may not be extended by subclasses.

The instanceof operator tests whether its left operand (an object) is an instance of its right operand (a class or interface). The result will be true if the right operand is the class or any superclass of the object, or any interface that it implements. Well-designed programs have very little need for the instanceof operator.

A Java source file may contain only one public class, though it may contain additional non-public classes. The name of the file must be the same as the name of the class, but with the .java extension.

Classes should be as self-contained and independent as it is reasonable to make them. The interface (the fields and methods it makes available outside the class) should be kept small.

An object is responsible for keeping itself in a valid state. Therefore, it should limit write access to essential fields.

Access

Variables and methods are accessed by name.

There are three dimensions to accessing names: namespaces, scope, and access modifiers.

Java uses six different namespaces: package names, type names, field (variable) names, method names, local variable names (including parameters), and labels. In addition, each declared enum has its own namespace. Identical names of different types do not conflict; for example, a method may be named the same as a local variable. However, it's best to avoid reusing names in this manner.

The scope of a name is the part of a class in which it can be seen.

Class variables and class methods (denoted by the keyword static) can be used anywhere within the class.

Instance variables and instance methods can only be used by a object (instance) of the correct type, using the syntax object.variableName or object.methodName(arguments). Within an instance method (but not in a static method), the keyword this refers to the object currently executing the method. this.variableName and this.methodName(arguments) may be abbreviated to variableName and methodName(arguments), respectively.

When an instance variable and a formal parameter have the same name, the name refers to the formal parameter; prefix the name with this. to refer to the instance variable.

To refer to a static variable or static method in the same class, just use its name. To refer to a class (static) name in a different class, use the syntax OtherClass.name. If you have an object obj of a class, you can also refer to static names in that class with the (confusing) syntax obj.name, which is an abbreviation for obj.getClass().name.

You can refer to a name (class or instance) in another class if and only if you have access privileges. Possible access privileges are:

You can refer to a name in a class in another package in either of two ways:

Methods

A method is a named executable chunk of code.

All executable statements must be in methods (or initializer blocks).

An initializer block is a block (zero or more statements enclosed in braces) within a class but not within a method. Initializer blocks are copied into every constructor. However, if preceded by the word static, an initializer block is executed only once, when the class is first loaded.

A method has a signature consisting of its name and the number and types of its parameters (also called arguments or actual parameters). The parameters in the declaration of the method are its formal parameters.

A method has a return type, which is not part of its signature. If the return type is other than void, then the method must return a value of the specified type.

A method may have local variables (also called method variables). These follow the scope rules, and are never available anywhere outside the method. The concepts static, public, protected, package, and private do not apply to local variables. Local variables have undefined values upon method entry. Formal parameters are a kind of local variable, but have initial values as supplied by the corresponding actual parameters.

Every method must have a signature that is unique within its class. Methods in other classes (even superclasses and subclasses) may have the same signature.

An instance method is executed by sending a message to an object. The message consists of: a reference to the object (typically its name), a dot, the name of the method, and zero or more actual parameters enclosed in parentheses. The object will respond by executing the corresponding method in the actual class of the object, which may be different from the type of the variable holding the object.

A class method is executed by sending a message to the class. The message consists of: the name of the class, a dot, the name of the method, and zero or more actual parameters enclosed in parentheses. The class will respond by executing the corresponding static method in that class.

When a message is sent, and before the method executes, the values of the actual parameters are copied into the corresponding formal parameters. Then the method body executes. Then the return value replaces the message, and all local names are forgotten.

Polymorphism

The two kinds of polymorphism are overloading and overriding.

Overloading occurs when a class declares two or more methods with the same name but different signatures. When a message is sent to an object or class with overloaded methods, the method with the best matching signature is the one that is used ("invoked").

Overriding occurs when a class declares a method with the same signature as that of an inherited method. When a message is sent to the object (or class, if it's a class method), the locally-defined method is the one that is used.

A class can declare a variable with the same name as an inherited variable, thus "hiding" or shadowing the inherited version. (This is like overriding, but for variables.)

Interfaces and Abstract Classes

The purpose of interfaces and abstract methods is to ensure that any classes derived from them will share the same set of methods.

An abstract method is a method that is declared but not defined. It is declared with the keyword abstract, and has a header like any other method, but ends with a semicolon instead of a method body.

An abstract class is one that contains one or more abstract methods; it must itself be declared with the abstract keyword. A class may be declared abstract even if it does not contain any abstract methods. A non-abstract class is sometimes called a concrete class.

An abstract class cannot be instantiated; that is, no objects of that class can be created. Instead, you can create subclasses that define (in the usual way) the inherited abstract methods, and these subclasses can be instantiated.

An interface is declared with the keyword interface instead of the keyword class. An interface may contain only public abstract methods and definitions of constants (that is, final variables). The keywords public and abstract before each method are optional.

A class may extend only one other class, but it may implement any number of interfaces. The syntax is: class Class extends Superclass implements Interface1, Interface2, .... When a class extends an interface, it may implement (define) some or all of the inherited abstract methods. A class must itself be declared abstract if it inherits abstract methods that it does not define.

A variable may be declared to have a type that is an abstract class or an interface; any object whose type implements or extends the variable's type may be assigned to that variable. The instanceof operator may take a class, abstract class, or interface as its right operand.

Inner Classes

An inner class is a class declared within another class. The four kinds of inner class are: (1) member class, (2) static member class, (3) local inner class, and (4) anonymous inner class. Unlike "outer" classes, the usual scope rules apply to inner classes.

A member class is defined at the top level of the class, along with fields and methods. It may have the same access modifiers as variables (public, protected, package, static, final), and is accessed in much the same way as variables of that class.

A static member class is defined like a member class, but with the keyword static. Despite its position inside another class, a static member class is actually an "outer" class--it has no special access to names in its containing class. To refer to the static inner class from a class outside the containing class, use the syntax OuterClassName.InnerClassName. A static member class may contain static fields and methods.

A local inner class is defined within a method, and the usual scope rules apply to it. It is only accessible within that method, therefore access restrictions (public, protected, package) do not apply. However, because objects (and their methods) created from this class may persist after the method returns, a local inner class may not refer to parameters or non-final local variables of the method.

An anonymous inner class is one that is declared and used to create one object (typically as a parameter to a method), all within a single statement. The anonymous inner class may either extend a class or implement an interface; the syntax is similar for both: new Super(parameters){methods} for classes and new Super(){methods} for interfaces, where Super is the name of the extended class or implemented interface, parameters are the parameters to the constructor for that class or interface (must be just () for interfaces), and methods override any inherited methods.

The keyword static may not be used within any inner class except a static member class.

Generics

A generic or parameterized class is one which takes one or more other classes as type parameters, using the syntax ClassName<Type1,Type2,...,TypeN>, where each of the Types is itself the name of a class. Such a genericized name can be used anywhere the "plain" name of the class can be used.

Any object type may be used as a type parameter, but primitive types cannot be used as type parameters.

Within a parameterized class, a type parameter name may be used almost anywhere the "plain" name of a class may be used. In particular, if Type is a type parameter, then variables may be declared to be of type Type, and methods may have parameters of type Type and return values of type Type. One restriction, however, is that new Type values cannot be created.

Parameterized classes were introduced in Java 5, and a number of pre-existing classes were parameterized. For backward compatibility, you can create and use objects of a parameterized class as if it were not parameterized (each type parameter is assumed to be Object), but the compiler will produce warning messages.

Enums

An enum is a class with a fixed number of instances that are defined within the class itself. Enums are used where there are a small, unchanging, predefined set of values, such as the days of the week. They have a number of advantages over using small integers: They provide better type safety, better readability, and they can be read in, printed out, and compared with == (as well as equals).

Every enum extends and inherits from java.lang.Enum. All constructors declared within an enum are automatically private. Since enums are classes, they can have methods, as well as constructors with parameters.

Every enum class creates a new namespace, so that the same name may be used as a value in different enum classes.