Java
Classes and Objects
Object-oriented languages such as Java are designed to simulate real-life objects (both tangible and abstract] by combining a group of data and operations on that data into classes. A class organizes attributes into data, or fields, and behavior into blocks of code, or methods. Fields and methods are called members of a class. A specific instance of a class can be declared as a variable in a program (unless the class is static), and this instantiation of the class is called an object.
Creating Classes
Classes are created using the class keyword followed by the name of the class. The class keyword is often preceded by a visibility modifier as well as other modifiers such as static or abstract (see Modifiers). The entire class description, its fields and methods, is enclosed in braces.
Examples:
-
class myClass { ... }
-
private class Node { ... }
Class Hierarchy
All Java classes fit into a hierarchy that relates classes in terms of the fields and methods that they share. Every class—except Object—is said to be a subclass of another class closer to the root of the hierarchy.
At the top of the hierarchy is the Object class, from which all other classes descend. Since all classes are derived from Object, an instance of any Java class can call the methods that Object can call: any object is an Object.
Inheritance: Every class inherits all of the methods and fields of its superclass, the class immediately above it in the class hierarchy. This means that any method that can be called on the superclass can be called on the subclass (see Polymorphism). The default superclass for any new class is Object, and Object does not need to be specified as a superclass. Use the extends keyword after the class name to allow a new class to inherit methods from a class other than Object.
Examples:
-
public class Rectangle extends Polygon { ... }
-
public class Square extends Rectangle { ... }
-
Every class (except Object) must extend exactly one class, although there is no limit to the number of interfaces it can implement (see Interfaces).
Static classes: A static class, denoted with the static keyword, is a class that cannot be instantiated. Its methods belong to the class, and are independent of a specific instantiation of an object. Static classes are called using the class name and the dot (method invocation) operator.
Example: The static class Java.lang.Math performs many of the mathematical functions of a scientific calculator.
These functions, such as the sine function and the natural logarithm function, exist outside of any particular object. The methods of the Math class are called as follows:
-
double cos = Math.cos( angle );
-
double rand = Math.random();
Abstract classes: An abstract class serves as a design template for subclasses to extend and fully implement or override some or all of its methods. Abstract classes are useful for designing a class hierarchy in which a superclass is too “abstract” to have specific implementations for all its methods. An abstract class does not require abstract methods, but a class with an abstract method must be declared abstract (see Abstract methods).
Programs With More Than One Class
Most Java programs use a combination of classes in the Java API and classes created by the programmer (which may also be extensions of classes in the API).
The import statement tells the Java interpreter which external classes a particular class will refer to. Without the import statement, Java expects methods and objects outside the current class to be fully qualified.
Examples:
-
import myClass /* myClass is in the same directory as the current class */
-
import java.util.StringTokenizer;
Each import statement is associated with only one class. To import all of the classes in a package, replace the class name with an asterisk:
Example:
-
import java.io.*; /* A class headed by thisstatement can refer to any of the classes in the java.io package. */
Packages: Packages are groups of related classes and interfaces that can be defined as a unit to form libraries. Classes that share a package have special visibility privileges with respect to one another (see Visibility modifiers). Use the package statement to associate a class with a package. The package statement must be the first line of a class file, and each class can be associated only with a single package:
-
package packageName;
Although package names are delineated by dots (e.g., java.util.regex), the dots do not signify any sort of subordinating relationship between the elements of the package name. Therefore, the statement import java.util.* does not import the objects in the java.util.regex package.
Nested classes: Classes (and interfaces) can be nested within any level of braces or within a method. Nested, or inner, classes allow related objects to be connected efficiently in terms of design and access. Nested classes can have static, final, or abstract modifiers just as any class; they can also be declared anonymous (see below). If a nested class is enclosed in a block of code, it is a local inner class, and is treated as a local variable. Otherwise, a nested class is treated as a member of the outer class and can access other members of the outer class.
Example:
-
class Car
class Transmission {
code for Transmission here
}
more code for Car here
}
Anonymous inner classes: An anonymous inner class is used to instantiate an object for immediate and onetime use without assigning it to a variable. An anonymous inner class is introduced with the new operator followed by a class name (the class’s constructor) and body:
Example:
-
Dice rollDice() {
return new Dice() {
int number = (int)( Math.random() * 6) + 1;
};
}
-
Java also supports anonymous inner interfaces, which must support all the methods that the outer interface supports.
Static Fields
Static fields, declared with the keyword static, are members that belong to the class rather than an instance of the class. Only one copy exists for all instances of a class. Static methods can reference only static fields (see Static classes).
Object Methods
The following is a partial list of the methods of java.lang.Object:
-
equals (Object obj): Returns true if obj “equals” the calling object. The programmer determines the meaning of “equals” in this context by overriding the method. If this method is not overridden, it returns true if the hashcode of obj equals the hashcode of the calling object. Unless either equals() or hashCode() has been overridden, this method is equivalent to using the == operator with two Objects as operands: equals() will return true if both operands refer to the same object.
-
getClass(): Returns the class of the calling object.
-
hashCode(): Returns a hashcode for the calling object.
-
clone(): Returns a copy of the calling object, provided the calling object implements the Cloneable interface.
-
toString(): Returns a String representation of the calling object. If this method is not overridden, it will return the calling object’s hashcode.
Classes and Objects

