Object types


Objects are defined by classes and by interfaces. A class defines what variables are in an object, and what methods can be used to access and manipulate those variables. An interface defines what methods must be in a class.

Sun provides literally thousands of standard classes for use in Java. The most commonly used class is String, in the java.lang package. The java.util package provides many other useful classes.

Every program you write will have additional classes which you define.

Here is a very simple example of a user-defined class:

public class Person {
    String name;
    int age;
    void birthday() {
        age = age + 1;

The class defines a new type, and the class name can be used just like any other type name. For example, just as you might say int count; you can say Person secretary;


Every class (except the built-in Object class) extends exactly one other class, and may implement any number of interfaces. The syntax is:

access ClassName extends SuperClassName
                 implements Interface1, ..., InterfaceN {

If you don't specify which class your class extends, it implicitly extends the Object class.

When a class extends another class, it inherits all the non-private fields and methods of that class. When a class implements an interface, it must provide all the methods specified by that interface (or must be declared abstract).